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

Cloudflare adapter dynamic routes #8827

Closed
eweren opened this issue Feb 1, 2023 · 3 comments
Closed

Cloudflare adapter dynamic routes #8827

eweren opened this issue Feb 1, 2023 · 3 comments

Comments

@eweren
Copy link

eweren commented Feb 1, 2023

Describe the bug

In it's current state the cloudflare adapter autogenerates excludes-routes based on the routes from the svelte-config. The issue with that is that if the app has (dynamic)routes that are longer than 100 characters the cloudflare build won't succeed because of it's limitations.

It would either be great to manually include those dynamic routes - since cloudflare accepts syntaxes like user/[name] - so that matching patterns won't be included by the adapter and instead only the manual dynamic route will be taken or that this dynamic route will be read from the sveltekit projects file structure.

Reproduction

Create a route that has more than 100 characters and try to build with cloudflare.

Logs

No response

System Info

System:
    OS: macOS 13.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 42.31 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 19.4.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.2.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 109.0.5414.119
    Firefox: 109.0
    Safari: 16.2
  npmPackages:
    @sveltejs/adapter-auto: 1.0.2 => 1.0.2 
    @sveltejs/adapter-cloudflare: 1.1.0 => 1.1.0 
    @sveltejs/kit: 1.2.10 => 1.2.10 
    svelte: 3.55.1 => 3.55.1 
    vite: 4.0.4 => 4.0.4

Severity

blocking an upgrade

Additional Information

No response

@Rich-Harris
Copy link
Member

exclude only contains static assets and prerendered routes, not functions for dynamically rendered routes. So I assume we're talking about prerendered routes here?

Unfortunately wildcards aren't a generally applicable solution, because they're greedy, and because 404 requests should be handled by the framework rather than the platform. I'm not sure there's really a solution we can implement; you might be better off coming up with a process to modify the generated _routes.json instead.

To be totally honest it feels like a slightly arbitrary platform limitation. As with the limit on the number of includes and excludes in _routes.json, it would be much simpler if we could simply tell Cloudflare to serve static assets without invoking a worker (previously: #8422 (comment)).

If I were you I would file a ticket with Cloudflare directly. In the meantime I'm going to take the liberty of cc'ing @jrf0110 since he's been active in this area before!

@Rich-Harris Rich-Harris added this to the later milestone Feb 1, 2023
@schuwa
Copy link

schuwa commented Feb 2, 2023

So I have written a short script that excludes some static generated routes for all those having the same problem:

import { readFileSync, writeFile } from "fs";

const staticPaths = [
  "/legal",
  "/auth",
  "/fonts",
  // Add your own paths...
];

const excludePathWildCard = staticPaths.map(e => `${e}/*`);

function run() {
    try {
      const rawRouteData = readFileSync("./.svelte-kit/cloudflare/_routes.json");
      const parsedRouteData = JSON.parse(rawRouteData.toString());
      const filteredExcludes = parsedRouteData.exclude.filter((item) => !staticPaths.some(path => item.startsWith(path)));

      const fixedRoutesJson = {
        ...parsedRouteData,
        exclude: [
          ...filteredExcludes,
          ...excludePathWildCard
        ]
      };
      writeFile("./.svelte-kit/cloudflare/_routes.json", JSON.stringify(fixedRoutesJson), () => {
        console.log("Finished fixing _routes.json");
      });
    } catch (e) {
      console.error("Failed to fix _routes.json:", e);
    }
} 

run();

But be careful about the matching function, so you only make the assets static that are server side generated.

@Rich-Harris
Copy link
Member

closed via #9111 — thanks @ajgeiss0702

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants