Skip to content

In dev, accessing a path not in src/routes causes the event.platform property to be undefined #12512

Closed as not planned
@yutak23

Description

@yutak23

Describe the bug

It is similar to the following issue, but differs in essence.
#10389

The PR allows the platform property to be emulated even in dev/preview, but when requesting a path that does not exist (which should be 404), the platform property is undefined.

I think the reason for this error is that event.platform is emulated only under the condition that if (route) is true.
I believe that this problem can be solved by implementing an implementation that emulates event.platform only for DEV.
https://github.com/sveltejs/kit/blob/%40sveltejs/kit%402.5.18/packages/kit/src/runtime/server/respond.js#L216

Reproduction

  1. Implement as follows.
// src/hooks.server.js
import { sveltekitSessionHandle } from 'svelte-kit-sessions';
import { sequence } from '@sveltejs/kit/hooks';
import KvStore from 'svelte-kit-connect-cloudflare-kv';
import { SESSION_SECRET } from '$env/static/private';
...
/** @type {import('@sveltejs/kit').Handle} */
const sessionHandle = async ({ event, resolve }) => {
	// https://kit.svelte.dev/docs/adapter-cloudflare#bindings
	const store = new KvStore({ client: event.platform.env.SESSION_NAMESPACE });
	const sveltekitSession = sveltekitSessionHandle({
		secret: SESSION_SECRET,
		saveUninitialized: true,
		cookie: {
			maxAge: 60 * 60 * 24 * 14, // 14 days
			secure: !dev,
			sameSite: 'lax'
		},
		store
	});

	return sveltekitSession({ event, resolve });
};

export const handle = sequence(sessionHandle, ...);
  1. Create wrangler.toml as follows
kv_namespaces = [
  { binding = "SESSION_NAMESPACE", id = "<ignored>", preview_id = "<ignored>" },
]
  1. Set svelte.config.js as follows
// svelte.config.js
import adapter from '@sveltejs/adapter-cloudflare';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	kit: {
		adapter: adapter({
			routes: {
				include: ['/*'],
				exclude: ['<all>']
			}
		}),
	}
};

export default config;
  1. npx vite dev
  2. Access paths that are not defined, e.g. /not_found
  3. error occur
TypeError: Cannot read properties of undefined (reading 'env')
    at sessionHandle (/home/study/workspace/pj/src/hooks.server.js:15:53)
    at apply_handle (/home/study/workspace/pj/node_modules/@sveltejs/kit/src/exports/hooks/sequence.js:85:11)
    at Object.eval [as handle] (/home/study/workspace/pj/node_modules/@sveltejs/kit/src/exports/hooks/sequence.js:74:10)
    at Module.respond (/home/study/workspace/pj/node_modules/@sveltejs/kit/src/runtime/server/respond.js:323:40)

Logs

No response

System Info

System:
    OS: Linux 5.14 AlmaLinux 9.3 (Shamrock Pampas Cat)
    CPU: (2) x64 Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
    Memory: 3.67 GB / 7.49 GB
    Container: Yes
    Shell: 5.1.8 - /bin/bash
  Binaries:
    Node: 18.20.2 - ~/.nodebrew/current/bin/node
    Yarn: 1.22.22 - ~/.nodebrew/current/bin/yarn
    npm: 10.5.0 - ~/.nodebrew/current/bin/npm
  npmPackages:
    @sveltejs/adapter-cloudflare: ^4.7.0 => 4.7.0 
    @sveltejs/kit: ^2.5.18 => 2.5.18 
    @sveltejs/vite-plugin-svelte: ^3.1.1 => 3.1.1 
    svelte: ^4.2.18 => 4.2.18 
    vite: ^5.3.5 => 5.3.5

Severity

serious, but I can work around it

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions