-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: static adapter errors during development #16506
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
Draft
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
80dbf60
poc
teemingc fd1b8d2
Merge branch 'feat-server-entrypoint' into prerender-dev
teemingc 010aa37
not needed
teemingc 114507e
prerender behaviour during development
teemingc 9720621
revert
teemingc f0d04ad
split into different pr
teemingc 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
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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { env } from 'sveltekit:env'; | ||
| import { Server } from 'sveltekit:server'; | ||
| import { manifest } from 'sveltekit:server-manifest'; | ||
| import { createReadableStream } from '@sveltejs/kit/node'; | ||
| import { styleText } from 'node:util'; | ||
| import { get_options_message } from '../utils.js'; | ||
| import { set_prerendering } from '$app/env/internal'; | ||
|
|
||
| set_prerendering(true); | ||
|
|
||
| /** @type {import('../../kit/src/types/internal.js').InternalServer} */ | ||
| const server = new Server(manifest); | ||
|
|
||
| await server.init({ | ||
| env, | ||
| read: (file) => createReadableStream(file) | ||
| }); | ||
|
|
||
| /** | ||
| * @param {Request} request | ||
| * @returns {Promise<Response>} | ||
| */ | ||
| export async function fetch(request) { | ||
| return await server.respond(request, { | ||
| // simulate prerendering during development to surface errors earlier | ||
| prerendering: { | ||
| dependencies: new Map(), | ||
| remote_responses: new Map(), | ||
| fallback: __SVELTEKIT_ADAPTER_STATIC_FALLBACK__ | ||
| }, | ||
| getClientAddress() { | ||
| throw new Error('Cannot read clientAddress on prerendered pages'); | ||
| }, | ||
| // @ts-expect-error this is only needed during actual prerendering | ||
| read: undefined, | ||
| before_handle: async (event, _config, prerender, handle) => { | ||
| if (!event.isSubRequest && !event.isDataRequest && !event.isRemoteRequest && !prerender) { | ||
| const error = new Error('Encountered dynamic routes'); | ||
| error.stack = ''; | ||
|
|
||
| console.error( | ||
| styleText( | ||
| ['bold', 'red'], | ||
| `@sveltejs/adapter-static: all routes must be fully prerenderable, but the ${event.url.pathname} route is dynamic\n` | ||
| ) | ||
| ); | ||
|
|
||
| console.log(get_options_message(!!Object.keys(event.params).length, false)); | ||
|
|
||
| throw error; | ||
| } | ||
| return await handle(); | ||
| } | ||
| }); | ||
|
Comment on lines
+36
to
+54
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. Need something public that lets us log errors when a page option isn't set |
||
| } | ||
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 @@ | ||
| declare global { | ||
| export const __SVELTEKIT_ADAPTER_STATIC_FALLBACK__: boolean; | ||
| } | ||
|
|
||
| export {}; |
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,4 @@ | ||
| { | ||
| "extends": "$app/tsconfig", | ||
| "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"] | ||
| } |
1 change: 1 addition & 0 deletions
1
packages/adapter-static/test/apps/prerendered/src/routes/no-prerender/+page.js
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 @@ | ||
| export const prerender = false; |
1 change: 1 addition & 0 deletions
1
packages/adapter-static/test/apps/prerendered/src/routes/no-prerender/+page.svelte
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 @@ | ||
| <p>not prerendered</p> |
7 changes: 7 additions & 0 deletions
7
packages/adapter-static/test/apps/prerendered/src/routes/query/+page.svelte
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,7 @@ | ||
| <script> | ||
| import { not_allowed } from './remote'; | ||
| const str = $derived(await not_allowed()); | ||
| </script> | ||
|
|
||
| <p>{str}</p> |
5 changes: 5 additions & 0 deletions
5
packages/adapter-static/test/apps/prerendered/src/routes/query/remote.js
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 @@ | ||
| import { query } from '$app/server'; | ||
|
|
||
| export const not_allowed = query(() => { | ||
| return 'foo'; | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * Returns a message with a list of options to correctly configure the app. | ||
| * @param {boolean} has_param_routes | ||
| * @param {boolean} has_custom_entries | ||
| * @returns {string} | ||
| */ | ||
| export function get_options_message(has_param_routes, has_custom_entries) { | ||
| const options = [ | ||
| 'add `export const prerender = true` to your root `+layout.js/.ts` or `+layout.server.js/.ts` file. This will try to prerender all pages.', | ||
| 'add `export const prerender = true` to any `+server.js/ts` files that are not fetched by page `load` functions.' | ||
| ]; | ||
|
|
||
| if (has_param_routes || has_custom_entries) { | ||
| let option = 'adjust the `prerender.entries` config option'; | ||
| if (has_param_routes) | ||
| option += ' (routes with parameters are not part of entry points by default)'; | ||
| options.push(option); | ||
| } | ||
|
|
||
| options.push( | ||
| 'set the `fallback` option — see https://svelte.dev/docs/kit/single-page-apps#usage for more info.', | ||
| "pass `strict: false` to `adapter-static` to ignore this error. Only do this if you are sure you don't need the routes in question in your final app, as they will be unavailable. See https://github.com/sveltejs/kit/tree/main/packages/adapter-static#strict for more info." | ||
| ); | ||
|
|
||
| let message = `You have the following options:${options.map((o) => `\n - ${o}`).join('')}`; | ||
| message += | ||
| "\n\nIf this doesn't help, you may need to use a different adapter. @sveltejs/adapter-static can only be used for sites that don't need a server for dynamic rendering, and can run on just a static file server."; | ||
| message += '\n\nSee https://svelte.dev/docs/kit/page-options#prerender for more details\n'; | ||
|
|
||
| return message; | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a public and easier way to do this