Skip to content
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 ignores page directives in +page.server.js #5926

Closed
acarl005 opened this issue Aug 16, 2022 · 4 comments · Fixed by #6012
Closed

SvelteKit ignores page directives in +page.server.js #5926

acarl005 opened this issue Aug 16, 2022 · 4 comments · Fixed by #6012
Labels
bug Something isn't working
Milestone

Comments

@acarl005
Copy link

Describe the bug

I have an app with pre-rendered pages. I'm NOT explicitly listing the pre-rendered pages in the svelte config, I'm letting the crawler discover them automatically. After migrating to 1.0.0-next.408 with the new routing, the pre-render crawler seems to be skipping pages if both of the following are true:

  1. the page is behind a redirect
  2. the page URL has a [param]

It was working before the routing overhaul. See the stackblitz link below for a repro. The /foo route redirects to /foo/bar which is handled by /foo/[slug]/+page.svelte. The /foo2 route redirects to /foo2/bar2 which DOES NOT have a param, it just resolves to /foo2/bar2/+page.svelte. Both page options are prerender = true, and both are linked from the home. The first is NOT prerendering but the second is. In the repro, run npm run build to see that 1 is getting prerendered which the other is not.

Before routing overhaul

This was working before the routing overhaul. Previously, I was doing my redirect within a standalone endpoint with something like

export async function GET() {
  return {
    headers: {
      location: `/newpath`
    },
    status: 302
  }
}

I also notice that the logs when I used to npm run build used to acknowledge my redirect:

10:05:52 AM: .svelte-kit/output/server/immutable/chunks/Container-0a471dd0.js             0.53 KiB
10:05:52 AM: .svelte-kit/output/server/immutable/chunks/EnrichedMarkdown-324634e1.js      11.89 KiB
10:05:52 AM: .svelte-kit/output/server/immutable/chunks/SchemaLabel-59931fc3.js           7.15 KiB
10:05:52 AM: .svelte-kit/output/server/immutable/chunks/hooks-3a5209ad.js                 1.72 KiB
10:05:52 AM: (node:1377) ExperimentalWarning: buffer.Blob is an experimental feature. This feature could change at any time
10:05:52 AM: (Use `node --trace-warnings ...` to show where the warning was created)
10:05:53 AM:   302 /course -> /course/lessons/what-is-sql
10:05:58 AM: 1/1 clients in student pool are ready
10:06:03 AM: 1/1 clients in student pool are ready
10:06:03 AM: vite v3.0.2 building for production...
10:06:03 AM: transforming...
10:06:03 AM: ✓ 2 modules transformed.
10:06:03 AM: rendering chunks...

But now this no longer seems to be happening.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-btjtae?file=src%2Froutes%2Ffoo2%2Fbar2%2F%2Bpage.js&terminal=dev

Logs

No response

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /bin/yarn
    npm: 7.17.0 - /bin/npm
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.64 
    @sveltejs/kit: next => 1.0.0-next.406 
    svelte: ^3.46.0 => 3.49.0

Severity

blocking an upgrade

Additional Information

No response

@Rich-Harris
Copy link
Member

There's a couple of things going on here. While prerendering, SvelteKit will bail on any page that is not prerenderable, which includes /foo because there's no export const prerender (and config.kit.prerender.default isn't true). Right now it only respects export const prerender in +page.js, not +page.server.js — that's a bug.

If you change it back to what you were doing before...

// src/routes/foo/+server.js
import { redirect } from '@sveltejs/kit';

/** @type {import('./$types').RequestHandler} */
export async function GET() {
  return new Response(undefined, {
    status: 302,
    headers: {
      location: '/foo/bar'
    }
  });
}

...then it will work, because it doesn't have a concept of 'prerenderable endpoints'. That's something we plan to change: #4093.

@acarl005
Copy link
Author

@Rich-Harris Thanks for the help. I changed it back redirect in a standalone endpoint instead of the page endpoint and now it works.

@Rich-Harris
Copy link
Member

re-opening until we fix the 'SvelteKit ignores page directives in +page.server.js' bug

@Rich-Harris Rich-Harris reopened this Aug 16, 2022
@benmccann benmccann changed the title Pre-render crawler doesn't follow redirect if page URL has a param SvelteKit ignores page directives in +page.server.js Aug 17, 2022
@benmccann benmccann added the bug Something isn't working label Aug 17, 2022
@benmccann benmccann added this to the 1.0 milestone Aug 17, 2022
@acarl005
Copy link
Author

Works great! Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants