Skip to content

Type error when passing dynamic asset paths to resolve() in SvelteKit 3 service worker #16802

Description

@JetLua

Describe the bug

With the latest SvelteKit 3, using resolve() with asset paths from src/service-worker/index.ts results in a TypeScript error.

The path property of the assets is typed as string, while resolve() appears to only accept a generated union of known route/path literals.

For example:

import { version } from '$app/env';
import { resolve } from '$app/paths';
import { immutable, assets } from '$app/service-worker';

const CACHE = `cache-${version}`;

const ASSETS = [
	...immutable.map((asset) => resolve(asset.path)),
	...assets.map((asset) => resolve(asset.path))
];

TypeScript reports:

Argument of type 'string' is not assignable to parameter of type
'[pathname: ""] | [pathname: "poem"] | [pathname: "poem/editor"] |
[pathname: "search"] | [pathname: `#${string}`] |
[pathname: `?${string}`] | [pathname: `poem#${string}`] |
[pathname: `poem/editor#${string}`] |
[pathname: `poem/editor?${string}`] |
[pathname: `poem?${string}`] |
[pathname: `search#${string}`] |

The error is reported at: resovle(asset.path)

because asset.path is typed as string, whereas resolve() expects its pathname argument to be one of the generated route literals.

workaround

const ASSETS: string[] = [
	...immutable.map((asset) => resolve<any>(asset.path)), // the Vite output
	...assets.map((asset) => resolve<any>(asset.path))  // everything in `static`
]

Reproduction

import { version } from '$app/env';
import { resolve } from '$app/paths';
import { immutable, assets } from '$app/service-worker';

const CACHE = `cache-${version}`;

const ASSETS = [
	...immutable.map((asset) => resolve(asset.path)),
	...assets.map((asset) => resolve(asset.path))
];

Logs

System Info

irrelevant

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions