kit: @sveltejs/adapter-cloudflare v2.2.0 always triggers hooks on build

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 (see hooks.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 to 2.1.0 to see it go away

Logs

> Using @sveltejs/adapter-cloudflare
file:///Users/my-app/node_modules/@sveltejs/kit/src/core/postbuild/fallback.js:53
        throw new Error(`Could not create a fallback page — failed with status ${response.status}`);
              ^

Error: Could not create a fallback page — failed with status 302
    at generate_fallback (file:///Users/my-app/node_modules/@sveltejs/kit/src/core/postbuild/fallback.js:53:8)
    at async process.<anonymous> (file:///Users/my-app/node_modules/@sveltejs/kit/src/utils/fork.js:25:17)
error during build:
Error: Failed with code 1
    at ChildProcess.<anonymous> (file:///Users/my-app/node_modules/@sveltejs/kit/src/utils/fork.js:68:13)
    at ChildProcess.emit (node:events:513:28)
    at ChildProcess.emit (node:domain:489:12)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:293:12)

System Info

System:
    OS: macOS 13.2.1
    CPU: (8) arm64 Apple M2
    Memory: 740.67 MB / 24.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.19.0 - ~/Library/Caches/fnm_multishells/80853_1678404247439/bin/node
    npm: 8.19.3 - ~/Library/Caches/fnm_multishells/80853_1678404247439/bin/npm
  Browsers:
    Brave Browser: 106.1.44.112
    Chrome: 111.0.5563.64
    Firefox: 103.0
    Safari: 16.3
  npmPackages:
    @sveltejs/adapter-auto: 2.0.0 => 2.0.0 
    @sveltejs/adapter-cloudflare: 2.1.0 => 2.1.0 
    @sveltejs/kit: 1.11.0 => 1.11.0 
    svelte: 3.55.1 => 3.55.1 
    vite: 4.1.4 => 4.1.4

Severity

breaks production builds, no work around except downgrading

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 9
  • Comments: 22 (8 by maintainers)

Most upvoted comments

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:

src/hooks.server.ts

// 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

@ajgeiss0702 @dbradleyfl Doh! You’re correct. I forgot the return. Works with the newest @sveltejs/adapter-cloudflare@2.2.2. Thank you much! if (building) return await resolve(event)

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:

src/hooks.server.ts

// 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

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 building brother - thank you!

Yup sounds like you’re not returning the result of await resolve(event). The return is the important part @figuerom16

Is there a temporary work around for this for Cloudflare Pages? I’ve tried downgrading to @sveltejs/adapter-cloudflare@2.1.0 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 if building stops execution if it is building