-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
@sveltejs/adapter-cloudflare v2.2.0 always triggers hooks on build #9386
Comments
I'm guessing this is the result of #9294. I'm not sure what it would make sense to do about this. Can you check |
Same issue here. I am now checking with the |
@Conduitry yeah, I had a feeling it was from #9294 as well based on it being the main change from 2.1.0 to 2.2.0. I could probably do that, but as @stefandevo says, it is a weird workaround/hack. I would assume that Kit would not run my hooks if I have no pre-rendering configured anywhere. For now I'll stay on 2.1.0 but if we decide to update I'll do the suggestion you offer, thanks 👍 |
I have also encountered this issue, with auth middleware as well. |
Same issue here. |
This seems like the best solution for now, unless an option is added to disable generation of a 404.html from the cloudflare adapter (which rich pushed back on before when I tried to add it so i'm not sure if that would happen. Although then neither of us could think of a case where it would make sense to be disabled, I wanted it "just in case" something like this happened) I would say the solution on the svelte side could be:
and/or
|
Instead of always generating a fallback page, I suggest that the cloudflare adapter copies over a static The benefits of this approach are:
Although this solution still doesn't really sit right with me, I feel that it is a good compromise. |
Just redirecting to the root doesn't seem like a good solution at all to me. Instead of getting a 404 page, users would just get redirected to the root page, which almost defeats the purpose of a 404 page at all. I feel the standard that works for most sites, and matches the functionality of other adapters is better than having a different behavior that doesn't make a ton of sense for most sites. It should either be an option, or just something that should be worked around (as the workaround is pretty simple. It should probably still be documented though) |
Perhaps the fallback page should also be opt-in and print a warning if it's not configured? Or perhaps we should document and warn users about their hooks whenever the fallback page generator fails (this would benefit the static adapter as well). In the end, it's difficult to get a behaviour consistent with other adapters until cloudflare removes the 100 rule limit of the _routes.json file. |
I've had to revisit this issue a couple different times since this hasn't been fixed and I keep forgetting why my sveltekit apps won't build in Pages but will locally 😞 The answer isn't explicitly here in code yet, so sharing what worked for me based on the above suggestions:
// other imports here
import { building } from '$app/environment'
export async function handle({ event, resolve }) {
if (building) {
const response = await resolve(event)
return response // bailing here allows the 404 page to build
}
// rest of the complex server code down here that was causing 404 page not to build |
Is there a temporary work around for this for Cloudflare Pages? I've tried downgrading to @sveltejs/[email protected] and using @dbradleyfl method if (building) await resolve(event), but I get the same "Error: Could not create a fallback page — failed with status 303" when deploying to Cloudflare Pages. |
It sounds like your site is still processing the hooks during build. Make sure that |
Yup sounds like you're not returning the result of await resolve(event). The return is the important part @figuerom16 |
@ajgeiss0702 @dbradleyfl Doh! You're correct. I forgot the return. Works with the newest @sveltejs/[email protected]. Thank you much! |
You're the GOAT, for anyone having this specific problem - this is the issue. Cloudflare Pages build failing due to auth redirect on base path. Figured i'd write it so this ranks on Google! Saved me time by putting me on to |
How do you add this if you're using sequence along with SvelteKitAuth? Update |
Do you have your source code for this? |
export const handle = building
? sequence()
: sequence(...); |
Yeah, nice one, I ended up doing: import { sequence } from "@sveltejs/kit/hooks";
import { building } from '$app/environment'
function optional (fn) {
if (building) {
return ({ event, resolve }) => resolve(event)
}
return fn
}
export const handle = optional(sequence()) |
Do you see any benefits from doing one to another? |
No, just a bit more readable I suppose. |
Can someone sanity check #11596 and tell me if it seems like a good fix for this? |
I wanted to add to this, I got the same issue, even though I was using I was getting an error relating to I had to wrap adding this to my Handle sequence like so: const sequences: Handle[] = [];
if (!building) {
sequences.push(i18n.handle({ disableAsyncLocalStorage: true }));
}
sequences.push(customHandle);
export const handle: Handle = sequence(...sequences); And now it works. what I don't understand is why wrangler is giving me issues when I'm using a different adapter I've got this function that helps me determine the correct adapter during runtime: function getAdapter() {
console.log('process.env.VITE_ADAPTER', process.env.VITE_ADAPTER);
switch (process.env.VITE_ADAPTER) {
case 'capacitor':
return staticAdapter({
strict: false,
pages: 'build/capacitor',
assets: 'build/capacitor',
fallback: 'index.html',
});
case 'cloudflare-workers':
return cloudflareWorkersAdapter();
default:
return adapterAuto();
}
} if I comment out the
This goes way above my head lol but i'm happy to help if there's any directions |
I recommend creating a new discussion (instead of an issue) with a minimal reproduction. It's difficult to diagnose the root cause of your problem from your current description. |
Thanks @eltigerchino I'm not sure what the discussion would be about but I've created a minimal reproducable: https://github.com/SaintPepsi/paraglide-sveltekit-example |
Describe the bug
Took a while to track down the cause of this, but I recently updated to adapter-cloudflare 2.2.0 and my builds started to break.
It appears a change from 2.1.0 to 2.2.0 now always triggers
hooks.server.js
to run, even if there are no configured prerenderable routes, which then triggers my auth middleware which redirects the user to another domain if they have no authentication cookie set (seehooks.server.js
in the repro repo).v2.1.0 works fine and I can build without the hook getting triggered by the adapter, but now in 2.2.0 it always triggers and thus breaks the build. AFAIK this means that there is no way to build for CloudFlare anymore if you have such an auth hook in place.
Reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-g2rzrj?file=src/hooks.server.js
Run
npm run build
to see the issue. Downgrade to2.1.0
to see it go awayLogs
System Info
Severity
breaks production builds, no work around except downgrading
Additional Information
No response
The text was updated successfully, but these errors were encountered: