-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: ISR support for adapter-vercel
#9063
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
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6ee8882
fix ancestor calculation
Rich-Harris 8dad723
ISR
Rich-Harris ea41d80
Update packages/adapter-vercel/index.js
Rich-Harris 9bee3f9
fix
Rich-Harris 8f686b8
dont expose group to users
Rich-Harris 60aa64d
dont involve isr config in splitting decisions
Rich-Harris d36f2d3
avoid shadowing
Rich-Harris 074761c
handle case where there is only one function
Rich-Harris d7fa303
handle isr on root route
dummdidumm 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 @@ | ||
| --- | ||
| '@sveltejs/adapter-vercel': patch | ||
| --- | ||
|
|
||
| fix: get ISR working on Vercel |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,22 +76,6 @@ const plugin = function (defaults = {}) { | |
| `${dirs.functions}/${name}.func`, | ||
| config | ||
| ); | ||
|
|
||
| if (config.isr) { | ||
| write( | ||
| `${dirs.functions}/${name}.prerender-config.json`, | ||
| JSON.stringify( | ||
| { | ||
| expiration: config.isr.expiration, | ||
| group: config.isr.group, | ||
| bypassToken: config.isr.bypassToken, | ||
| allowQuery: config.isr.allowQuery | ||
| }, | ||
| null, | ||
| '\t' | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -158,6 +142,22 @@ const plugin = function (defaults = {}) { | |
| /** @type {Map<string, string>} */ | ||
| const functions = new Map(); | ||
|
|
||
| // every route needs a `group`, so that a page and its __data.json are invalidated | ||
| // as a pair. automatically assign group ids to routes that don't have one | ||
| let group_id = 0; | ||
|
|
||
| // ensure automatically assigned group ids are unique | ||
| for (const route of builder.routes) { | ||
| if (route.prerender === true) continue; | ||
|
|
||
| if (route.config?.isr?.group !== undefined) { | ||
| group_id = route.config.isr.group; | ||
| } | ||
| } | ||
|
|
||
| /** @type {Map<import('@sveltejs/kit').RouteDefinition<import('.').Config>, import('.').ServerlessConfig['isr']>} */ | ||
| const isr_config = new Map(); | ||
|
|
||
| // group routes by config | ||
| for (const route of builder.routes) { | ||
| if (route.prerender === true) continue; | ||
|
|
@@ -175,6 +175,14 @@ const plugin = function (defaults = {}) { | |
|
|
||
| const config = { runtime, ...defaults, ...route.config }; | ||
|
|
||
| if (config.isr) { | ||
| if (config.isr.group === undefined) { | ||
| config.isr.group = ++group_id; | ||
| } | ||
|
|
||
| isr_config.set(route, config.isr); | ||
| } | ||
|
|
||
| const hash = hash_config(config); | ||
|
|
||
| // first, check there are no routes with incompatible configs that will be merged | ||
|
|
@@ -238,12 +246,61 @@ const plugin = function (defaults = {}) { | |
| src = '^/?'; | ||
| } | ||
|
|
||
| src += '(?:/__data.json)?$'; | ||
|
|
||
| const name = functions.get(pattern); | ||
| if (name) { | ||
| static_config.routes.push({ src, dest: `/${name}` }); | ||
| functions.delete(pattern); | ||
| const isr = isr_config.get(route); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't have any route config, we'll never get into here because
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catch. fixed |
||
| if (isr) { | ||
| if (isr.allowQuery?.includes('__pathname')) { | ||
| throw new Error('__pathname is a reserved query parameter'); | ||
|
Rich-Harris marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| const base = `${dirs.functions}/${route.id.slice(1)}`; | ||
| builder.mkdirp(base); | ||
|
|
||
| const target = `${dirs.functions}/${name}.func`; | ||
| const relative = path.relative(path.dirname(base), target); | ||
|
|
||
| // create a symlink to the actual function, but use the | ||
| // route name so that we can derive the correct URL | ||
| fs.symlinkSync(relative, `${base}.func`); | ||
| fs.symlinkSync(`../${relative}`, `${base}/__data.json.func`); | ||
|
|
||
| let i = 1; | ||
| const pathname = route.segments | ||
| .map((segment) => { | ||
| return segment.dynamic ? `$${i++}` : segment.content; | ||
| }) | ||
| .join('/'); | ||
|
|
||
| const json = JSON.stringify( | ||
| { | ||
| expiration: isr.expiration, | ||
| group: isr.group, | ||
| bypassToken: isr.bypassToken, | ||
| allowQuery: ['__pathname', ...(isr.allowQuery ?? [])], | ||
| passQuery: true | ||
| }, | ||
| null, | ||
| '\t' | ||
| ); | ||
|
|
||
| write(`${base}.prerender-config.json`, json); | ||
| write(`${base}/__data.json.prerender-config.json`, json); | ||
|
|
||
| const q = `?__pathname=/${pathname}`; | ||
|
|
||
| static_config.routes.push({ | ||
| src: src + '$', | ||
| dest: `${route.id}${q}` | ||
| }); | ||
|
|
||
| static_config.routes.push({ | ||
| src: src + '/__data.json$', | ||
| dest: `${route.id}/__data.json${q}` | ||
| }); | ||
| } else { | ||
| static_config.routes.push({ src: src + '(?:/__data.json)?$', dest: `/${name}` }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -400,22 +457,21 @@ async function create_function_bundle(builder, entry, dir, config) { | |
| } | ||
| } | ||
|
|
||
| const files = Array.from(traced.fileList); | ||
|
|
||
| // find common ancestor directory | ||
| /** @type {string[]} */ | ||
| let common_parts = []; | ||
| let common_parts = files[0]?.split(path.sep) ?? []; | ||
|
|
||
| for (const file of traced.fileList) { | ||
| if (common_parts) { | ||
| const parts = file.split(path.sep); | ||
| for (let i = 1; i < files.length; i += 1) { | ||
| const file = files[i]; | ||
| const parts = file.split(path.sep); | ||
|
|
||
| for (let i = 0; i < common_parts.length; i += 1) { | ||
| if (parts[i] !== common_parts[i]) { | ||
| common_parts = common_parts.slice(0, i); | ||
| break; | ||
| } | ||
| for (let i = 0; i < common_parts.length; i += 1) { | ||
| if (parts[i] !== common_parts[i]) { | ||
| common_parts = common_parts.slice(0, i); | ||
| break; | ||
|
Rich-Harris marked this conversation as resolved.
Outdated
|
||
| } | ||
| } else { | ||
| common_parts = path.dirname(file).split(path.sep); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.