docker - svelteKit hooks.ts when deployed on cloudrun - Stack Overflow

When running the sveltekit app locallyhooks.ts:import { redirect, type Handle } from '@sveltejs

When running the sveltekit app locally hooks.ts:

import { redirect, type Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ event, resolve }) => {
    const { pathname } = event.url;

    console.log('Full URL:', event.url);
    console.log('Pathname:', pathname);

    if (pathname.startsWith('/events')) {
        throw redirect(301, '/');
    }

    if (pathname.startsWith('/nl')) {
        throw redirect(301, '/');
    }

    if (pathname === '/privacy-policy') {
        throw redirect(301, '/privacy');
    }

    if (pathname.startsWith('/terms')) {
        throw redirect(303, '/terms.pdf');
    }

    if (pathname.startsWith('/support')) {
        throw redirect(303, '/contact');
    }

    console.log('No redirect triggered.');

    return await resolve(event);
};

When running pnpm dev all redirects work fine.

When deployed to a URL on cloud run, I get a page not found (and no logs - so not reaching hooks.ts?)

Cloud logs:

textPayload: "SvelteKitError: Not found: /support
    at resolve2 (file:///app/build/server/index.js:4909:18)
    at file:///app/build/server/index.js:4731:19
    at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
    at with_event (file:///app/build/server/index.js:1868:22)
    at resolve (file:///app/build/server/index.js:4729:11)
    at init_promise.#options.hooks.handle (file:///app/build/server/index.js:4985:71)
    at file:///app/build/server/index.js:4724:28
    at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
    at with_event (file:///app/build/server/index.js:1868:22)
    at respond (file:///app/build/server/index.js:4722:28) {"
    timestamp: "2025-03-24T15:31:35.625761Z"

Because it redirects fine locally, I am a bit confused why this is not working right now. Any ideas?

When running the sveltekit app locally hooks.ts:

import { redirect, type Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ event, resolve }) => {
    const { pathname } = event.url;

    console.log('Full URL:', event.url);
    console.log('Pathname:', pathname);

    if (pathname.startsWith('/events')) {
        throw redirect(301, '/');
    }

    if (pathname.startsWith('/nl')) {
        throw redirect(301, '/');
    }

    if (pathname === '/privacy-policy') {
        throw redirect(301, '/privacy');
    }

    if (pathname.startsWith('/terms')) {
        throw redirect(303, '/terms.pdf');
    }

    if (pathname.startsWith('/support')) {
        throw redirect(303, '/contact');
    }

    console.log('No redirect triggered.');

    return await resolve(event);
};

When running pnpm dev all redirects work fine.

When deployed to a URL on cloud run, I get a page not found (and no logs - so not reaching hooks.ts?)

Cloud logs:

textPayload: "SvelteKitError: Not found: /support
    at resolve2 (file:///app/build/server/index.js:4909:18)
    at file:///app/build/server/index.js:4731:19
    at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
    at with_event (file:///app/build/server/index.js:1868:22)
    at resolve (file:///app/build/server/index.js:4729:11)
    at init_promise.#options.hooks.handle (file:///app/build/server/index.js:4985:71)
    at file:///app/build/server/index.js:4724:28
    at AsyncLocalStorage.run (node:internal/async_local_storage/async_hooks:91:14)
    at with_event (file:///app/build/server/index.js:1868:22)
    at respond (file:///app/build/server/index.js:4722:28) {"
    timestamp: "2025-03-24T15:31:35.625761Z"

Because it redirects fine locally, I am a bit confused why this is not working right now. Any ideas?

Share Improve this question asked Mar 24 at 15:50 RiëlRiël 1,3313 gold badges17 silver badges32 bronze badges 5
  • It gives a 404 in Google Cloudrun, and the /support if obviously not in the routes path. It just needs to redirect to /contact – Riël Commented Mar 24 at 15:52
  • i would try to debug locally, running the docker container, and then navigate to docker ui (the easiest) click on the runnung container and click on the tab "Files" and explore the container see if the "/contact" or "/support" are present. also, have a look here svelte.dev/docs/kit/adapters it depends what adapter you are using, each has its own caveats. feel free to update the question, adding new information, it does help a lot – Ion Utale Commented Mar 24 at 16:21
  • Those folders are not present, but also locally NON docker, but then I just run non-containerized! That might be the issue. Non containerized those folders are not needed, it just redirects properly – Riël Commented Mar 24 at 19:26
  • unforrtunatedly, the hooks.ts is never called in the docker container - it should log the path at least. If I build locally, and run node build/index.js it works. If I run pnpm dev it works (paths are logged), but in the container, that error. No path logs. – Riël Commented Mar 24 at 20:36
  • can you share the Dockerfile? – Ion Utale Commented Mar 25 at 18:30
Add a comment  | 

1 Answer 1

Reset to default 1

All works as expected when renaming the file to hooks.server.ts

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744242263a4564760.html

相关推荐

  • docker - svelteKit hooks.ts when deployed on cloudrun - Stack Overflow

    When running the sveltekit app locallyhooks.ts:import { redirect, type Handle } from '@sveltejs

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信