sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.73k stars 1.94k forks source link

`adapter-static` and nonprerendered `+layout/page.server.js` files #7637

Open Nickersoft opened 2 years ago

Nickersoft commented 2 years ago

Describe the bug

Hey folks,

I ran into a bit of an edge-case recently while trying to make a hybrid web + Tauri application (web uses adapter-node, Tauri creates an SPA using adapter-static). If there exists a +page.server.js or +layout.server.js file that exports a load() function, running the SPA via a basic web server will throw a 500 internal error, complaining that /__data.json cannot be found. I'm curious if this is an intended bug or if there is a workaround to it.

image image

Reproduction

  1. Clone this repo and this branch.
  2. Run pnpm install.
  3. Run pnpm build.
  4. Using a basic web server tool (I used http-server), start a server (http-server build).
  5. Attempt to load in the web browser

Moving or deleting the +layout.server.js file will fix the issue.

Logs

[2022-11-13T23:31:19.546Z]  "GET /" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
(node:83494) [DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated
(Use `node --trace-deprecation ...` to show where the warning was created)
[2022-11-13T23:31:19.576Z]  "GET /_app/immutable/start-39da41f2.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.578Z]  "GET /_app/immutable/chunks/index-4ce6debf.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.579Z]  "GET /_app/immutable/chunks/singletons-eb12c85b.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.589Z]  "GET /_app/immutable/chunks/0-bc3f1161.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.590Z]  "GET /_app/immutable/chunks/_layout-b03384d3.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.592Z]  "GET /_app/immutable/components/layout.svelte-3895ce33.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.592Z]  "GET /_app/immutable/chunks/1-f4967476.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.596Z]  "GET /_app/immutable/chunks/2-9d7f729d.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.597Z]  "GET /_app/immutable/components/error.svelte-52072039.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.600Z]  "GET /_app/immutable/components/pages/_page.svelte-6481b931.js" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.603Z]  "GET /__data.json" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.604Z]  "GET /__data.json" Error (404): "Not found"
[2022-11-13T23:31:19.607Z]  "GET /__data.json" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
[2022-11-13T23:31:19.607Z]  "GET /__data.json" Error (404): "Not found"
[2022-11-13T23:31:19.611Z]  "GET /_app/version.json" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"

System Info

System:
    OS: macOS 12.3.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 112.78 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 18.9.0 - ~/.asdf/installs/nodejs/18.9.0/bin/node
    npm: 8.19.1 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Brave Browser: 107.1.45.123
    Safari: 15.4
  npmPackages:
    @sveltejs/adapter-static: 1.0.0-next.48 => 1.0.0-next.48 
    @sveltejs/kit: 1.0.0-next.544 => 1.0.0-next.544 
    svelte: ^3.53.1 => 3.53.1 
    vite: ^3.2.3 => 3.2.3

Severity

blocking all usage of SvelteKit

Additional Information

Obviously this isn't completely blocking, from a web standpoint, but it is blocking me from creating a production build of my Tauri app, as I can't create the SPA and my web version (which SSR'd) needs the cookie information passed from +layout.server.js.

dummdidumm commented 2 years ago

If you're creating an SPA, there's no runtime on the server, just some static files that are returned when they are requested specifically. This means that +layout.server.js can't work when not prerendered, as there's neither a server runtime nor a prerendered __data.json output. So this behavior is intended, although it would be nice to somehow detect this better and throw a more meaningful error, ideally earlier on, or at least document this somehow.

Nickersoft commented 2 years ago

Yeah, I figured as much :/ From what I can tell, it looks like as long as there is a .server.ts file, even if there's nothing in it, the site will break. Agreed it would be nice to have a way to catch this earlier or avoid it altogether without just removing the whole file. Took me quite a long while to debug, as I didn't know why it would be looking for a __data.json file as an SPA haha.

Nickersoft commented 1 year ago

@dummdidumm Is there any kind of workaround for this? Is my only option temporarily renaming +layout.server.js during the build and then naming it back afterwards? I realized even the mere existence of +layout.server.js causes the output to look for a __data.json file.

dummdidumm commented 1 year ago

"the mere existinnce of a layout.server causes to look for a __data file" is tracked in #7967