kit: Build process fails after attempting to prerender auth endpoints

Describe the bug

I’m working on a PWA that prerenders the root page endpoint located inside an (app) layout group (so the service worker has access to the app shell) with a fetch in (app)/+layout.ts (which is NOT prerendered) that authenticates the user and redirects to a sign-in page at run-time.

This somewhat hacky setup was producing the desired behavior until version 1.27.4 - namely a prerendered index.html app shell that attempts authentication (along with fetching various cacheable assets) only at run-time and makes related data from the parent (app) layout load function accessible to subsequent load functions inside the group.

After version 1.27.4, the build process errors out because it now seems to attempt to authenticate (and redirect) at build time:

node:internal/event_target:1033
  process.nextTick(() => { throw err; });
                           ^
Error: 404 /api/validate (fetched from /)
To suppress or handle this error, implement `handleHttpError` in https://kit.svelte.dev/docs/configuration#prerender

Reviewing the build output, the index.html is now referencing the sign-in page redirected to on failed auth attempts. If I explicitly add export const prerender = false to this endpoint, the error instead becomes

Error: /api/(auth)/validate is not prerenderable

The errors looks similar to https://github.com/sveltejs/kit/issues/11031 and likewise seems related to the changes introduced here - the issue started in version 1.27.4 and downgrading to 1.27.3 fixes it. It still persists in version 1.27.6 after related issues with fully-static sites seems to have been closed.

Reproduction

Unfortunately a simple reproduction is not feasible due to the dependencies on auth etc.; hopefully the info provided + related issues are enough for a start. If I manage to narrow things down to where a reproduction is feasible I’ll post an update.

Logs

No response

System Info

System:
  OS: Linux 6.2 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
  CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
  Memory: 14.34 GB / 62.71 GB
  Container: Yes
  Shell: 5.1.16 - /bin/bash
Binaries:
  Node: 18.18.0 - ~/.nvm/versions/node/v18.18.0/bin/node
  npm: 9.8.1 - ~/.nvm/versions/node/v18.18.0/bin/npm
  pnpm: 8.9.0 - ~/.nvm/versions/node/v18.18.0/bin/pnpm
npmPackages:
  @sveltejs/adapter-vercel: ^3.0.3 => 3.0.3 
  @sveltejs/kit: ^1.27.6 => 1.27.6 
  svelte: ^4.2.3 => 4.2.3 
  vite: ^4.5.0 => 4.5.0

Severity

blocking an upgrade

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Reactions: 1
  • Comments: 22 (16 by maintainers)

Most upvoted comments

@austerj @gyurielf I’ve submitted a fix that restores the previous behaviour prerendering a shell page when SSR is turned off and there is no server load function for that page. This avoids running load functions during prerendering if there are no server loads we need to prerender the __data.json file for. Let me know what you guys think!

I’m wondering if the old behaviour should be reimplemented as another prerender option such as prerender = ‘spa’ or if the fix should just be reverted entirely.

Just wondering if the issue is still on the radar or if there’s anything I can do from my side to help? Unfortunately not that familiar with SvelteKit internals, but I’m a bit worried about potentially being stuck on 1.27.3 indefintely, since I haven’t managed to find another way to prerender the index.html shell while keeping the authentication fetches at run-time and by now (8 months into development) switching frameworks would be a huge pain.