kit icon indicating copy to clipboard operation
kit copied to clipboard

adapter-cloudflare: only including `_app` content in `_routes.json`

Open userquin opened this issue 2 years ago • 15 comments

Describe the bug

The adapter should include any assets inside the client output folder instead _app only (kit.appDir).

Latest changes in kit allow plugins to emit/generate assets in the client folder: @vite-pwa/sveltekit, sitemap...

The problem is here: https://github.com/sveltejs/kit/blob/master/packages/adapter-cloudflare/index.js#L42 (should include the client folder)

Reproduction

~~no, sorry, just reported from Discord here: https://discord.com/channels/937808017016119440/937973377883336704/1041084393239891988~~

https://github.com/hazre/neosvr-mod-manager/tree/fix/pwa

Logs

No response

System Info

NA

Severity

blocking all usage of SvelteKit

Additional Information

The app is using the new @vite-pwa/sveltekit plugin to add the PWA, cloudflare returns 404 for the service worker and webmanifest assets.

userquin avatar Nov 14 '22 11:11 userquin

Related kit changes:

  • https://github.com/sveltejs/kit/pull/5416
  • https://github.com/sveltejs/kit/pull/5620
  • https://github.com/sveltejs/kit/pull/5601#issuecomment-1188391404

userquin avatar Nov 14 '22 13:11 userquin

Here's a list of the cloudflare folder. Assets are definitely there and they work fine locally with pnpm run preview but when I deploy it to cloudflare pages, I get a 404s, which is weird.

https://gist.github.com/hazre/1ebf72e8f43f0252822076aa6c33b3c6

hazre avatar Nov 14 '22 22:11 hazre

@jrf0110 tagging you in case you or anyone else from Cloudflare might be interested in giving this one a look. We no longer have any Svelte maintainers employed at Cloudflare and I don't have a Cloudflare account so couldn't be very confident in any attempt I made

benmccann avatar Nov 14 '22 23:11 benmccann

When deploying to Cloudflare with kit 1.0.0-next.544 I don't get any functions deployed. do I understand correctly that this issue related to that?

tedsteen avatar Nov 15 '22 11:11 tedsteen

@benmccann I'll get some 👀 on this today! Thanks for the ping

jrf0110 avatar Nov 15 '22 16:11 jrf0110

@tedsteen that sounds like a separate issue

Rich-Harris avatar Nov 15 '22 16:11 Rich-Harris

started looking at this, wrote up notes in https://github.com/sveltejs/kit/pull/7667

Rich-Harris avatar Nov 15 '22 17:11 Rich-Harris

Just to clarify, the _routes.json file is an optimization. Missing entries should not cause any assets to 404

jrf0110 avatar Nov 15 '22 19:11 jrf0110

Just to clarify, the _routes.json file is an optimization. Missing entries should not cause any assets to 404

Yeah, sorry for that, maybe we can change the title and the content: I just check the code, I never use CloudFlare, I assume the routes file being used for routing.

userquin avatar Nov 15 '22 19:11 userquin

Generated _routes.json sent by @hazre after I created this issue:

{
  "version": 1,
  "description": "Generated by @sveltejs/adapter-cloudflare",
  "include": [
    "/*"
  ],
  "exclude": [
    "/_app/immutable/*",
    "/favicon-16x16.png",
    "/favicon-32x32.png",
    "/favicon.ico"
  ]
}

userquin avatar Nov 15 '22 19:11 userquin

Is there a bug to report here? Is the listed severity correct?

blocking all usage of SvelteKit

jrf0110 avatar Nov 15 '22 20:11 jrf0110

Am I correct in assuming this would also fix the version.json calls from counting towards the quota on CF? Because I seem to be running into the problem where if one (or a few) users leave open their tab, it will do thousands of function requests, quickly ramping up costs.

Neicul123 avatar Nov 19 '22 10:11 Neicul123

Yes. Alternative this line... https://github.com/sveltejs/kit/blob/284b20bb543890e7d5a6ca99581d6eba72d9deae/packages/adapter-cloudflare/index.js#L81 ...would need to lose the immutable part.

@jrf0110 is there any likelihood that we could just exclude all static assets (perhaps behind an option in the adapter), without adding them individually to the exclude array (and exceeding the 100 limit, in many cases)?

Rich-Harris avatar Nov 19 '22 20:11 Rich-Harris

I'm unsure if this is related, but any generated items are not added to the manifest.

I'm running into a similar issue to the one mentioned here (https://github.com/xiphux/svimg/issues/16#issuecomment-1201612762). This package outputs transformed images to a subfolder inside the cloudflare directory, like so: image

All of the images in the generated folder return 404 when trying to access them, but the two untransformed images in the images/ folder are accessible.

However, none of the generated images are in the manifest, which seems to be the reason why they aren't served. image

If I made the build run twice, it will output these files to the manifest, which allows Cloudflare to recognize and serve the files. I feel the solution to this could be adding generated files to the manifest after running other preprocess steps.

0xMihir avatar Nov 24 '22 18:11 0xMihir

Is there a bug to report here? Is the listed severity correct?

blocking all usage of SvelteKit

Only a slight exaggeration. Currently, almost any request that goes through would count as a function invocation. My site is mostly static except for a few (4) endpoints that must have server-side code. According to Cloudflare analytics, my site gets 1-1.5 million (uncached) requests on an average day (and is growing). The endpoints only get ~10,000 requests per day.

This issue is the difference between my side-project being free and it being yet another subscription I have to pay for (seemingly unnecessarily, since the sections that actually need functions take up <1% of the total requests).

My site is growing, so I would probably have to pay for it at some point, but when I do, it would be much better if I only had to pay for the function requests I actually use, instead of the ones just serving static content.

Currently, I'm using svelte-multi-adapter to use both the static adapter and the nodejs adapter. I'm then deploying the static site to cloudflare pages, (for most of the content on the website) then the nodejs generated server to a vps I have to host somewhere else. Obviously, this setup is not ideal.

ajgeiss0702 avatar Nov 29 '22 01:11 ajgeiss0702