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

Build failed for adapter Cloudflare #132

Closed
Scorpio3310 opened this issue Jul 30, 2023 · 3 comments
Closed

Build failed for adapter Cloudflare #132

Scorpio3310 opened this issue Jul 30, 2023 · 3 comments

Comments

@Scorpio3310
Copy link

Scorpio3310 commented Jul 30, 2023

Hi,
I wanted to build an app with the Cloudflare adapter. I used only this example (https://github.com/sveltekit-i18n/lib/tree/master/examples/locale-router-advanced) in my project, and when I try to build the app, the build fails.
Any ideas on how can I solve this?

Full code in attachment.


Using @sveltejs/adapter-cloudflare

node:internal/event_target:1006
process.nextTick(() => { throw err; });
^
Error: Could not create a fallback page — failed with status 500
at generate_fallback (file:///Users/nik/Sites/language-test/my-app/node_modules/@sveltejs/kit/src/core/postbuild/fallback.js:53:8)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async MessagePort. (file:///Users/nik/Sites/language-test/my-app/node_modules/@sveltejs/kit/src/utils/fork.js:22:16)
Emitted 'error' event on Worker instance at:
at [kOnErrorMessage] (node:internal/worker:290:10)
at [kOnMessage] (node:internal/worker:301:37)
at MessagePort. (node:internal/worker:202:57)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:731:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28)

Node.js v18.12.0


my-app.zip

@Scorpio3310 Scorpio3310 changed the title Build failed for adapter cloudflare Build failed for adapter Cloudflare Jul 30, 2023
@jarda-svoboda
Copy link
Member

Hi @Scorpio3310!
You need to handle fallback routes Cloudflare adapter uses (http://sveltekit-prerender/[fallback], or http://sveltekit-prerender/de/[fallback] etc..) to prerender error pages. Optionally you could disable prerendering..

// hooks.server.js
/** @type {import('@sveltejs/kit').Handle} */
export const handle = async ({ event, resolve }) => {

  // ...

  try {
    // Fetch the redirected route
    const response = await fetch(redirectTo, request);
    
    // Get response body and set html headers
    const data = await response.text();
    
    // Serve the redirected route.
    // In this case we don't have to set the html 'lang' attribute
    // as the default locale is already included in our app.html.
    return new Response(data, {
      ...response,
      headers: {
        ...response.headers,
        'Content-Type': isDataRequest ? 'application/json' : 'text/html',
      },
    });
  } catch (error) {
    // In case of error redirect to the error page.
    return resolve({ ...event, locals: { lang: locale } }, {
      transformPageChunk: ({ html }) => html.replace('%lang%', `${locale}`),
    })
  }

}

@Scorpio3310
Copy link
Author

Scorpio3310 commented Aug 1, 2023

@jarda-svoboda thank you it's working! 💯

Another question. I have set export const defaultLocale = 'cs'; to 'cs' but it's stills redirect me to 'en'.
How to change this?

@jarda-svoboda
Copy link
Member

jarda-svoboda commented Aug 1, 2023

Well, because your request.headers.get('accept-language') is (or fallbacks to) en.

So update hooks.server.js to change this behavior if needed..)

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

No branches or pull requests

2 participants