You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a web app that has a mix of prerendered pages and dynamic pages. The app also contains a REST endpoint that has export const prerender = true. The prerendered endpoint fetches data from a CMS, containing a bunch of metadata needed for a another endpoint that depends on. This fetch is done at build time, as the metadata itself doesn't change very often (but often enough that it can't be a static file), so we slapped the prerender flag on it.
Basic layout:
src/routes/[...slug] Pages from the CMS
src/routes/api/route1 Component endpoint to get some data. Depends on route2.json
src/routes/api/route2.json Prerendered metadata from the CMS
route1 requests data from route2.json and does stuff with it. All of this works great in dev mode, but it stops working in preview mode and also when deployed to the host. route2.json actually works fine when accessing thru the browser; however, it does not work when using event.fetch() from within route1. Instead it returns the HTML from our 404 page. route2.json also works without the prerender, but that means it needs to fetch the data from the CMS at runtime, which is what we're trying to avoid.
Browser output: Result: {"something":true,"somethingElse":true} (working as intended here)
Shutdown dev
Run npm run build && npm run preview
Access http://localhost:4173/
Browser output: Result: {"something":true} (not working as intended here)
Manually
Create a new SvelteKit app
Create a route for /api/route2.json that is prerender = true that outputs some JSON
Create a route for /api/route1 that outputs some JSON,
Have /api/route use event.fetch to retrieve data from /api/route2.json, and merge the JSON objects
Create a new route at root named [...slug] with param file containing:
export const match = (param) => {
return /^[a-z0-9-]+$/.test(param);
};
Under [...slug]/+page.svelte Do a fetch at onMount fetching /api/route1 . Output the response to body to see what comes back from the fetch.
Expected result:
Output from /api/route1 has output from itself and from /api/route2.json in dev mode and preview mode.
Actual results:
Output from /api/route1 has output from itself and from /api/route2.json in dev mode, but preview mode throws an error.
If /api/route2.json is accessed from the client, the output is correct, it's only incorrect when accessing the endpoint using event.fetch. Error is thrown (see logs). When doing event.fetch('/api/route2.json'), it's not routing correctly, instead it's fetching from the "slug" endpoint returning HTML, not JSON.
Logs
Node logs:
SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON
at JSON.parse (<anonymous>)
at parseJSONFromBytes (node:internal/deps/undici/undici:5329:19)
at successSteps (node:internal/deps/undici/undici:5300:27)
at fullyReadBody (node:internal/deps/undici/undici:1447:9)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async specConsumeBody (node:internal/deps/undici/undici:5309:7)
at async GET (file:///D:/Projects-Ethode/sk-predender-test/.svelte-kit/output/server/entries/endpoints/api/route1/_server.js:8:3)
at async render_endpoint (file:///D:/Projects-Ethode/sk-predender-test/.svelte-kit/output/server/index.js:260:20)
at async resolve2 (file:///D:/Projects-Ethode/sk-predender-test/.svelte-kit/output/server/index.js:2802:22)
at async respond (file:///D:/Projects-Ethode/sk-predender-test/.svelte-kit/output/server/index.js:2697:22)
The text was updated successfully, but these errors were encountered:
x0rsw1tch
changed the title
Global slug overrides prerendered route when accessed from another route doesn't work with
Global slug overrides prerendered route when accessed from another route, but ok directly
Sep 30, 2024
x0rsw1tch
changed the title
Global slug overrides prerendered route when accessed from another route, but ok directly
Global slug overrides prerendered route when accessed from another route with event.fetch, but ok from client?
Sep 30, 2024
All of this works great in dev mode, but it stops working in preview mode and also when deployed to the host.
Note that prerendering doesn't occur until build time so during development the behaviour is inaccurate.
I suspect the catch-all route is incorrectly taking precedence over the static files.
EDIT: after building, the prerendered files are missing from the server manifest so they aren't being fetched when given a relative URL on the server-side. Additionally, the catch-all route is taking precedence here, so the request never falls through to the prerendered endpoint.
As a workaround for now. You should exclude the /api routes from the catch all. For example:
All of this works great in dev mode, but it stops working in preview mode and also when deployed to the host.
Note that prerendering doesn't occur until build time so during development the behaviour is inaccurate.
I suspect the catch-all route is incorrectly taking precedence over the static files.
EDIT: the prerendered files are missing from the server manifest so they aren't being fetched when given a relative URL on the server-side. Additionally, the catch-all route is taking precedence here, so the request never falls through to the prerendered endpoint.
As a workaround for now. You should exclude the /api routes from the catch all. For example:
Describe the bug
I have a web app that has a mix of prerendered pages and dynamic pages. The app also contains a REST endpoint that has
export const prerender = true
. The prerendered endpoint fetches data from a CMS, containing a bunch of metadata needed for a another endpoint that depends on. This fetch is done at build time, as the metadata itself doesn't change very often (but often enough that it can't be a static file), so we slapped the prerender flag on it.Basic layout:
route1
requests data fromroute2.json
and does stuff with it. All of this works great in dev mode, but it stops working in preview mode and also when deployed to the host.route2.json
actually works fine when accessing thru the browser; however, it does not work when usingevent.fetch()
from withinroute1
. Instead it returns the HTML from our 404 page.route2.json
also works without the prerender, but that means it needs to fetch the data from the CMS at runtime, which is what we're trying to avoid.I have made a repo to demonstrate the problem: https://github.com/x0rsw1tch/sk-route-prerender-test. Reproduction steps included.
Reproduction
Using minimal reproduction repo
npm i && npm run dev
http://localhost:5173/
Result: {"something":true,"somethingElse":true}
(working as intended here)npm run build && npm run preview
http://localhost:4173/
Result: {"something":true}
(not working as intended here)Manually
/api/route2.json
that isprerender = true
that outputs some JSON/api/route1
that outputs some JSON,/api/route
useevent.fetch
to retrieve data from/api/route2.json
, and merge the JSON objects[...slug]
with param file containing:[...slug]/+page.svelte
Do a fetch at onMount fetching/api/route1
. Output the response to body to see what comes back from the fetch.Expected result:
Output from
/api/route1
has output from itself and from/api/route2.json
in dev mode and preview mode.Actual results:
/api/route1
has output from itself and from/api/route2.json
in dev mode, but preview mode throws an error./api/route2.json
is accessed from the client, the output is correct, it's only incorrect when accessing the endpoint usingevent.fetch
. Error is thrown (see logs). When doingevent.fetch('/api/route2.json')
, it's not routing correctly, instead it's fetching from the "slug" endpoint returning HTML, not JSON.Logs
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: