kit: Simply having a `+page.server.ts` page crashes static builds
Describe the bug
500 Internal Error
Reproduction
- Create a
src/routes/+page.server.tsfile with this content
import type { PageServerLoad } from './$types';
export const load = (async () => {
return;
}) satisfies PageServerLoad;
- Make a build with
adapter-static - Open it with
http-server
Repro: https://github.com/NatoBoram/bug-report-sveltekit-ssr-csr
Logs
GET
http://127.0.0.1:8081/
[HTTP/1.1 404 Not Found 1ms]
TypeError: can't access property "then", a.default.detectStore(...) is undefined
h1-check.js:1:1301
XHRGET
http://127.0.0.1:8081/__data.json?x-sveltekit-invalidated=01
[HTTP/1.1 404 Not Found 1ms]
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data app.306aa39f.js:1:5729
System Info
System:
OS: Linux 6.2 Pop!_OS 22.04 LTS
CPU: (12) x64 AMD Ryzen 5 5600X 6-Core Processor
Memory: 12.92 GB / 31.26 GB
Container: Yes
Shell: 5.8.1 - /usr/bin/zsh
Binaries:
Node: 20.4.0 - /usr/bin/node
npm: 9.8.0 - ~/.local/share/pnpm/npm
pnpm: 8.6.6 - ~/.local/share/pnpm/pnpm
Browsers:
Chrome: 114.0.5735.198
npmPackages:
@sveltejs/adapter-auto: ^2.1.0 => 2.1.0
@sveltejs/adapter-node: ^1.3.1 => 1.3.1
@sveltejs/adapter-static: ^2.0.2 => 2.0.2
@sveltejs/kit: ^1.20.4 => 1.22.1
svelte: ^4.0.0 => 4.0.5
vite: ^4.3.6 => 4.4.1
Severity
- Blocks all usage of SvelteKit for apps that need both
adapter-nodeandadapter-static
Additional Information
The workflow I was aiming for is:
- Catch a
localecookie in+layout.server.ts - In
+layout.ts, return either the server’s cookie or the browser’s cookie - In
+layout.svelte, setup a store with the cookie, put it in a context, use that context to translate the app
Because of that, it seems impossible to make SSR + CSR apps that have authentication via cookies; you either have to completely disable SSR or completely disable static builds, which is unacceptable.
Related
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 15 (8 by maintainers)
Commits related to this issue
- Reproduce bug https://github.com/sveltejs/kit/issues/10332 — committed to NatoBoram/bug-report-sveltekit-ssr-csr by NatoBoram 3 years ago
Not being able to use them is fine by me, the issue is that it crashes the web page.
When I read Adapter static assumes you have completely static output, no node server running, I understand that the rendered web page will assume you have completely static output, no node server running. But in reality, it doesn’t actually assume that because a request to
__data.json?x-sveltekit-invalidated=01was made.Not only that, but there’s a whole section about when not to pre-render
So it seems like pre-rendering is not suitable to applications with user authentification.
Essentially, what I’m saying is that
adapter-staticshould behave exactly as if you were running this:And even if you were using
datain+page.ts, it would just come out asnulland you would be able to account for that and have an application that you can deploy on both a server with SSR and on a static file host like GitHub Pages.But really, the elephant in the room is that simply having a
+page.server.tsfile shouldn’t crash the page. Requests to__data.json?x-sveltekit-invalidated=01should be completely disabled in static builds.You can’t use non-prerenderes
.serverfiles with adapter static. Adapter static assumes you have completely static output, no node server running. We should make that more obvious through a clear error message.I was able to fix this issue by exporting those options in my
layout.jsinstead oflayout.server.js. So it indeed seems that.serverfiles are causing the calls to__data.jsonwith adapter-static.I like that solution.
This way, if I set
export const prerender = falsebut still useadapter-static, then I’m clearly saying that the data from that+page.server.tsor+layout.server.tsshouldn’t exist at runtime and to not fetch it. In my+layout.tsor+page.ts,datashould beundefinedornulland I should be able to continue as normal.Someone could still enable pre-rendering on these files by simply adding
export const prerender = true, in which casedatashould return whatever was done during build time.I tried to do that to import data for example from the filesystem. In
page.ts, I cannot import modules that only work on NodeJS, inpage.server.tsI can. Also, in the load function ofpage.ts, the data property mentions that this comes from eitherpage.server.tsorlayout.server.ts(does not work withlayout.server.tsthough).I can accomplish the same by creating a server endpoint that is called with
fetchfrompage.ts. But just usingpage.server.tsseemed simpler.Is this something that should work or should
page.server.tsbe avoided when usingadapter-static?