sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
18.11k stars 1.84k forks source link

event.platform is undefined when deploying on Vercel #12324

Open alexbjorlig opened 3 weeks ago

alexbjorlig commented 3 weeks ago

Describe the bug

When I deploy my project on Vercel, this line gives undefined:

export const GET: RequestHandler = async (event) => {

    console.log('is platform here?', event.platform); // Why is event.platform undefined here?
    return json({ message: 'GET request to the homepage', platformDefined: !!event.platform });
}

Do I need to do something different?

My svelte.config.js looks like this:

import adapter from '@sveltejs/adapter-vercel';

import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

/** @type {import('@sveltejs/kit').Config} */
const config = {
    // Consult https://kit.svelte.dev/docs/integrations#preprocessors
    // for more information about preprocessors
    preprocess: vitePreprocess(),

    kit: {
        adapter: adapter({})
    }
};

export default config;

I also tried adding a console.log in src/hooks.server.ts like this:

import type { Handle } from '@sveltejs/kit';

export const handle: Handle = async ({ event, resolve }) => {

    console.log('hooks-server:', {platform: event.platform});
    const response = await resolve(event);
    return response;
};

But this was also undefined?

Reproduction

repo to clone is here.

Deployed vercel project is here.

Logs

is platform here? undefined

### System Info

```Shell
System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M1 Max
    Memory: 444.77 MB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.12.2 - ~/.nvm/versions/node/v20.12.2/bin/node
    npm: 10.8.1 - ~/.nvm/versions/node/v20.12.2/bin/npm
    pnpm: 8.15.4 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 125.0.6422.142
    Safari: 17.5
  npmPackages:
    @sveltejs/adapter-vercel: ^5.3.1 => 5.3.1 
    @sveltejs/kit: ^2.5.10 => 2.5.10 
    @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.1.1 
    svelte: ^4.2.7 => 4.2.18 
    vite: ^5.0.3 => 5.2.13

Severity

annoyance

Additional Information

CleanShot 2024-06-11 at 22 06 51@2x

CleanShot 2024-06-11 at 22 11 01@2x

dummdidumm commented 3 weeks ago

What are you using platform for? It's only defined for edge functions, because its only member, context, is only available within edge functions (settable through the runtime adapter option). The only member of context is waitUntil, which you can now import directly in both edge and serverless functions: https://vercel.com/changelog/waituntil-is-now-available-for-vercel-functions

@Rich-Harris should we "shim" this for serverless, by passing along a "fake" context on which we provide the waitUntil method? I tend towards "no, use the package Vercel provides directly". If you're using platform you're already provider-specific.

alexbjorlig commented 3 weeks ago

What are you using platform for? It's only defined for edge functions, because its only member, context, is only available within edge functions. The only member of context is waitUntil, which you can now import directly in both edge and serverless functions: https://vercel.com/changelog/waituntil-is-now-available-for-vercel-functions

@Rich-Harris should we "shim" this for serverless, by passing along a "fake" context on which we provide the waitUntil method? I tend towards "no, use the package Vercel provides directly". If you're using platform you're already provider-specific.

It was exactly waitUntil. Ok, so the recommended approach is to import it from here import { waitUntil } from '@vercel/functions'?

I'm not sure why, but I was under the impression that such thing should live on the "platform", but importing from @vercel/functions would also be ok.

Rich-Harris commented 3 weeks ago

I tend towards "no, use the package Vercel provides directly"

I lean the same way - it would just add code to people's bundles if they weren't using it. For consistency it might even be better to remove it from edge functions? (deprecate with warning first, obvs)

dummdidumm commented 3 weeks ago

Ok, so the recommended approach is to import it from here import { waitUntil } from '@vercel/functions'?

Exactly

I'm not sure why, but I was under the impression that such thing should live on the "platform"

platform is more for things that are per-request and coming from the underlying platform. But yeah it's not very strict what's coming from where. In this case, since Vercel provides it, it's better to use the package directly.

For consistency it might even be better to remove it from edge functions? (deprecate with warning first, obvs)

Makes sense

eltigerchino commented 3 weeks ago

Isn't it already populated here? https://github.com/sveltejs/kit/blob/f67898d25ee32e9377221979d2a9e6b792786f4e/packages/adapter-vercel/files/edge.js#L13-L24 The ambient type for it exists too although it's not correctly included when importing the adapter at the moment. https://github.com/sveltejs/kit/blob/f67898d25ee32e9377221979d2a9e6b792786f4e/packages/adapter-vercel/ambient.d.ts#L1-L12 Or am I mistaken and thinking of something entirely different?

dummdidumm commented 3 weeks ago

It's there, but it's only set for edge functions, not serverless functions, hence the confusion and thought to remove the context for edge, since it only contains waitUntil. Argument against it is that at some point in the future there might be other things on the context object and we would go adding it back in again - which makes me think we just leave everything as is and close this issue.