-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
hooks.js example causes a build error #763
Comments
Ah, yep, this makes sense. In the |
Ran across an issue with the
import { ensureAdmin } from '$lib/ensureAdmin.ts';
export async function handle(request, render) {
const { path, headers } = request;
if (/\/admin/.test(path) && !ensureAdmin(request)) {
return { status: 403 };
}
return render(request);
} I tried looking at the different environments from Maybe if there was an env for prerendering / adapting? |
Yeah, I think |
I was poking around in the code adjacent to the If I am grokking the codebase correctly it seems like this would be the first env flag that would be unique to SvelteKit runtime. It looks like the kit/packages/kit/src/core/adapt/index.js Lines 9 to 19 in 8805c6d
Though, it does not look like the Lines 100 to 104 in 24fab19
Edit: Sorry for blasting this issue, but I did a little more digging and found that Vite does some pretty heavy handed work for ensuring the Any thoughts? I'm happy to make a pull request. |
In the process of trying to figure out how we'd tackle this, I ended up writing the code, so I took the liberty of opening a PR: #833 |
That's awesome, thanks for taking the time to move this along! |
I'm assuming this was fixed since #833 was merged, but let me know if that's incorrect. I was having a bit of a hard time following whether there were two things being discussed here or just one |
I have to investigate if my problem is actually this... but, i updated to edit: I can still reproduce with this simple
error:
|
yeah, the issue isn't fixed, #833 just enables a workaround. There's a couple of solutions I can think of:
I think I prefer 2: if (!page_config.prerender) {
return {
status: 204,
headers: {
'x-svelte-kit-prerendered': 'false'
}
};
} Now that I think about it, using a header like this would be a neat way to avoid the undocumented export async function handle(request, render) {
const response = await render(request);
return {
status: response.status,
headers: response.headers,
body: transform(response.body)
// oops, omitted the 'dependencies' array, you just broke prerendering
};
} Is |
Yeah, having an undocumented top-level key in the response object that people need to carry along without knowing about seems risky. Sticking that somewhere in For these secret API things, what about using a key name that's not a valid HTTP header name, just to make it clear that something special is happening here, and to avoid any sort of conflict? |
According to https://stackoverflow.com/a/3569667, which is a summary of the relevant IETF RFC, the following characters are invalid HTTP header names:
So we could do something like |
That sounds reasonable to me. 👍 |
Sorry to be annoying, but I'm still getting the same error with exmple
shows:
|
Similar to the above comment, I get an error about headers now when running > Using @sveltejs/adapter-node
TypeError: Cannot read property 'headers' of undefined
at Object.handle (file:///Users/me/sveltekit-demo/.svelte/output/server/app.js:1924:19)
at async ssr (file:///Users/me/sveltekit-demo/.svelte/output/server/app.js:1711:12)
at async visit (file:///Users/me/sveltekit-demo/node_modules/@sveltejs/kit/dist/chunks/index5.js:531:20)
at async prerender (file:///Users/me/sveltekit-demo/node_modules/@sveltejs/kit/dist/chunks/index5.js:682:5)
at async Builder.prerender (file:///Users/me/sveltekit-demo/node_modules/@sveltejs/kit/dist/chunks/index5.js:743:4)
at async adapt (/Users/me/sveltekit-demo/node_modules/@sveltejs/adapter-node/index.js:26:4)
at async adapt (file:///Users/me/sveltekit-demo/node_modules/@sveltejs/kit/dist/chunks/index5.js:765:2)
at async file:///Users/me/sveltekit-demo/node_modules/@sveltejs/kit/dist/cli.js:604:5
> 500 /about
Error: 500 /about Using:
|
Can confirm that this is still happening on This issue should unlock the first steps to the CSP issue I think. I haven't tackled the |
I am also encountering this issue. It happens at build time. The functions One more side-effect of this issue. I am initializing my DB Connection at the top of the |
I'm happy to report that i'm now able to build. Same version.. So something I was doing was triggering this. I don't know enough about the internals of SK, but I have a hunch it has to do with this bug - because once I cleaned up all the instances of this, I tried the hooks handle() again, and it built. |
Same. Seems to be fixed in |
config.kit.ssr has been removed — use the handle hook instead: https://kit.svelte.dev/docs#hooks-handle. |
hi, I've solved restarting from the svelte@next boilerplate:
|
Describe the bug
When the following
hooks.js
file exists,npm run build
throws an error. (Howevernpm run dev
works without error and the site runs perfectly.)To Reproduce
src/hooks.js
npm run build
.The command will throw this error:
about.svelte
is nothing special:svelte.config.cjs
contains adapter as:adapter: node(),
.Information about your SvelteKit Installation:
Severity
Minor
The text was updated successfully, but these errors were encountered: