Skip to content

Types aren't generated for +page.server.ts unless there is a corresponding +page.svelte #6044

@daleweaver777

Description

@daleweaver777

Describe the bug

I have a route that I don't need any component rendered for. I just need to do some work in the load function of a +page.server.ts file and then return a redirect (or throw error for +error.svelte to catch). This currently works but I ran into a small bug. If I don't have an empty +page.svelte file in addition to my +page.server.ts file, no types are generated in .svelte-kit/types/src/routes folder. I therefore can't import type { PageServerLoad } from './$types';

I tried using a +server.ts file for this but the functionality recently changed to not allow +error.svelte to catch thrown errors from a +server.ts files. See issue #5993 and PR #6028

+page.server.ts

import { error, redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';

const shortcodes: { [index: string]: string } = {
	microsoft: 'https://www.microsoft.com',
	google: 'https://www.google.com'
};

export const load: PageServerLoad = ({ params }) => {
	if (!params.shortcode || !shortcodes[params.shortcode]) {
		throw error(404, 'Shortcode not found');
	}

	return redirect(301, shortcodes[params.shortcode]);
};

Reproduction

Create a +page.server.ts file without a +page.svelte file. Check if any types were generated by npm run dev in .svelte-kit/types/src/routes

Logs

No response

System Info

System:
    OS: macOS 12.5
    CPU: (10) arm64 Apple M1 Max
    Memory: 5.49 GB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.7.0 - /opt/homebrew/bin/node
    npm: 8.16.0 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 104.1.42.95
    Chrome: 104.0.5112.79
    Firefox Developer Edition: 104.0
    Safari: 15.6
    Safari Technology Preview: 16.0
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.64
    @sveltejs/kit: next => 1.0.0-next.417
    svelte: ^3.44.0 => 3.49.0
    vite: ^3.0.0 => 3.0.8

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions