-
-
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
Sveltekit 2 + adapter-node 2 unable to disable prerender, '$env/dynamic/private' used in hooks.server.ts #11341
Comments
|
I'll need to test if there's another package allowing those to resolve for me. Or it could be the difference between In either case, |
(as expected, this error will also show up if you like me have imported some other module in your |
probably caused by: see this comment: |
You can use the import { building } from '$app/environment'
import { env } from '$env/dynamic/private';
const fallback = 'http://localhost:3067'
export const DBHOST = building ? fallback : env.DBHOST || 'http://localhost:3067' Alternatively, you can avoid referencing it outside of the hooks |
@CaptainCodeman - thanks for steering me to this issue. It was exactly my problem. I was going crazy trying to figure out what would need to be prerendered since my +layout.server.ts disabled it. As @ollema experienced, I had a `import { query } from '$lib/server/db' in my hooks.server.ts. The db module naturally needs to get the database URL. Using the $app/environment's building worked for me. |
Spent a while not resolving this today before checking GitHub. We've used a file Thanks @CaptainCodeman for the fix. |
I agree it would have been helpful to see hooks in the stack trace. Would have helped me find the problem. |
Personally, I think any .server code should be able to access any env values, whatever combination of I think it's fairly common to want to create clients for databases etc... based on env values, inside server hooks and server load fns. Having to add code to short circuit things just for the build risks introducing bugs because the code becomes more complex that it should really need to be. The other solution is to lazy-initialize clients, inside hooks, so the reference isn't in any immediately imported code ... but again, it's extra complexity that I'd prefer to avoid. |
I have similar case https://github.com/xmlking/spectacular/blob/main/apps/console/src/lib/variables/variables.server.ts |
This solution doesn't work for me. |
Okay, so { env } and using env.DATABASE_URL instead of { DATABASE_URL } seems to fix the issue for me. I am using SSR, the default rendering option. |
Describe the bug
Migration to SvelteKit 2, updated adapter-node to v2. Modified src/routes/+layout.server.ts to
export const prerender = false;
and addedconfig.kit.prerender.crawl = false
andconfig.kit.prerender.entries
= [] to the svelte config.Importing a dynamic
$env/dynamic/private
in hooks.server.ts because the variable is optional (typically use dynamic variables for items not strictly sensitive, e.g. username, hosts, ids, where the default value is committed to the repo).Interim fix: using process.env will work.
Reproduction
https://stackblitz.com/edit/envdynamicprivate?file=src%2Fhooks.server.js
Run
npm run build
to see the error message.Logs
System Info
Severity
annoyance
Additional Information
As stated, we can use
process.env
as a stop gap until this is resolved. My assumption is that the check for$env/dynamic/private
needs to run after the declaration of prerender false.hooks.server.js
loads before prerender value is established.The text was updated successfully, but these errors were encountered: