-
Notifications
You must be signed in to change notification settings - Fork 161
Feat Automatic cdn invalidation #665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
conico974
merged 10 commits into
opennextjs:main
from
conico974:feat/automatic-cdn-invalidation
Jan 17, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2482a35
basic implementation
conico974 81194cf
install and biome fix
conico974 55feb54
cloudfront implementation
conico974 723372e
review fix
conico974 55123b9
fix & comment
conico974 fcc2851
update to use initialPath when needed
conico974 a200cc8
fix build
conico974 21ee706
fix linting
conico974 3223f53
changeset
conico974 f1d92e4
review fix
conico974 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@opennextjs/aws": minor | ||
--- | ||
|
||
Add an override to automatically invalidate the CDN (not enabled by default) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/open-next/src/overrides/cdnInvalidation/cloudfront.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
CloudFrontClient, | ||
CreateInvalidationCommand, | ||
} from "@aws-sdk/client-cloudfront"; | ||
import type { CDNInvalidationHandler } from "types/overrides"; | ||
|
||
const cloudfront = new CloudFrontClient({}); | ||
export default { | ||
name: "cloudfront", | ||
invalidatePaths: async (paths) => { | ||
const constructedPaths = paths.flatMap( | ||
({ initialPath, resolvedRoutes }) => { | ||
const isAppRouter = resolvedRoutes.some( | ||
(route) => route.type === "app", | ||
); | ||
// revalidateTag doesn't have any leading slash, remove it just to be sure | ||
const path = initialPath.replace(/^\//, ""); | ||
return isAppRouter | ||
? [`/${path}`, `/${path}?_rsc=*`] | ||
conico974 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
: [ | ||
`/${path}`, | ||
`/_next/data/${process.env.NEXT_BUILD_ID}${path === "/" ? "/index" : `/${path}`}.json*`, | ||
]; | ||
}, | ||
); | ||
await cloudfront.send( | ||
new CreateInvalidationCommand({ | ||
DistributionId: process.env.CLOUDFRONT_DISTRIBUTION_ID!, | ||
InvalidationBatch: { | ||
// Do we need to limit the number of paths? Or batch them into multiple commands? | ||
Paths: { Quantity: constructedPaths.length, Items: constructedPaths }, | ||
CallerReference: `${Date.now()}`, | ||
}, | ||
}), | ||
); | ||
}, | ||
} satisfies CDNInvalidationHandler; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { CDNInvalidationHandler } from "types/overrides"; | ||
|
||
export default { | ||
name: "dummy", | ||
invalidatePaths: (_) => { | ||
return Promise.resolve(); | ||
}, | ||
} satisfies CDNInvalidationHandler; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.