Node import broken when using redirect
Describe the bug
Server-side only code in preload seems to work fine if directly loaded, but breaks when using redirection.
This is particularly relevant when using node modules such as fs, the page will then work as expected if opened by the user, but not if redirected from the website itself.
There does not seem to be an easy way to use fs to prefetch resources in the ssr-part of the code, except to use if(typeof window === 'undefined') and do manual management oneself
https://github.com/sveltejs/sapper/issues/1627 seems to provide a convoluted solution to the problem, https://github.com/sveltejs/sapper/issues/917 does talk about it but from a store perspective while it seems more general, and https://github.com/sveltejs/sapper/issues/1347 seems to hint at a design solution?
Logs When using fs in ssr:
'fs' is imported by src/routes/redirect.svelte, but could not be resolved – treating it as an external dependency
To Reproduce https://github.com/joy-void-joy/sapper-bug-node-import
Expected behavior
Behavior between first load and reload should be consistent. So if it is expected for it to crash, it should always do so, although I would rather it always worked.
Above all, there should probably be something in the documentation in that regard, or a more convenient way to use preload. I can work on a pull request if something is agreed on
Information about your Sapper Installation:
- The output of
npx envinfo --system --npmPackages svelte,sapper,rollup,webpack --binaries --browsers:
System:
OS: Linux 5.9 Arch Linux
CPU: (4) x64 Intel(R) Pentium(R) CPU 5405U @ 2.30GHz
Memory: 326.34 MB / 3.65 GB
Container: Yes
Shell: 5.8 - /bin/zsh
Binaries:
Node: 15.2.0 - /usr/bin/node
npm: 6.14.8 - /usr/bin/npm
Browsers:
Firefox: 82.0.3
npmPackages:
rollup: ^2.3.4 => 2.33.3
sapper: ^0.28.0 => 0.28.10
svelte: ^3.17.3 => 3.29.7
-
Your browser: chromium
-
Your hosting environment (i.e. Local, GCP/AWS/Azure, Vercel/Begin, etc...): local
-
If it is an exported (
npm run export) or dynamic application: Dynamic application (npm run dev)
Severity A little, as it seems a huge inconvenience for something that server-side-rendering should provide. There does seem to exist alternatives to this issue, but none feel as simple as it needs to be
Hey there, im not part of the Sapper crew, but this is as designed ... see https://sapper.svelte.dev/docs#Preloading
When you going to the /redirect page directly the preload function is running in the server side, however when you go via the anchor tag from the index page, its running in the browser, hence the fs module not being available.
The key bit there is this ...
Note that preload will run both on the server side and on the client side. It may therefore not reference any APIs only present in the browser.
even though that comment is talking about browser API's .. the same could be said about calling server side only API's.
If you want to expose data to your pages in the way you seem to be describing, you need to create an API type endpoint and then you use fetch to request it in the preload function. see https://sapper.svelte.dev/docs#Server_routes you could do the equivalent where the data comes from something you access with the fs library