kit icon indicating copy to clipboard operation
kit copied to clipboard

SvelteKit generates `__data.json` request even if `+page.*` does not export a `load` function

Open ivanhofer opened this issue 3 years ago • 1 comments

Describe the bug

A SvelteKit application makes a request to __data/json even when no actual load function gets exported inside +page.server.js.

I would expect that no unnecessary request gets made as it should be clear to SvelteKit that it can't fetch any data from that endpoint.

Someone could argue if an empty +page.server.ts file makes sense at all. Maybe SvelteKit should throw an error if it sees that a +page.server.ts file does not export anything that is useful to SvelteKit. I could imagine there will be some users that create a load function, but forget to export it. In this case an error message would be really helpful before they have to figure out what they did wrong.

Of course this would make sense for all other +*.ts files

Reproduction

Repo: https://stackblitz.com/edit/sveltejs-kit-template-default-djcepj?file=src/routes/test/+page.server.ts

  1. start the application
  2. hover over the link
  3. check the logs, a request to /test/__data.json was made, even if +page,server,ts does not export a load function

Logs

No response

System Info

StackBlitz

Severity

annoyance

Additional Information

No response

ivanhofer avatar Dec 06 '22 13:12 ivanhofer

I've just come across the same issue. In my case, I use +page.server.ts to exports actions but don't need a server-side load function.

I've worked around it by moving the actions to a dedicated route that results in a 404 if accessed via GET.

siim-m avatar Dec 06 '22 19:12 siim-m

Thanks!

ivanhofer avatar Jan 26 '23 20:01 ivanhofer

It would be great if SvelteKit detected these cases during build, f.e. when building as a static website, and printed a helpful message that a *.server.ts files don't work with static builds, etc.

(SvelteKit 1.7.2 here)

trusktr avatar Feb 19 '23 23:02 trusktr