Skip to content

Cannot redefine property: fetch #3356

Closed
@GauBen

Description

@GauBen

Describe the bug

I'm trying to import a built svelte-kit site within another one, and it is not possible because importing import { handler } from 'sub-site/build/handler' has side-effects, namely calling __fetch_polyfill.

Reproduction

  1. Create a svelte-kit website that uses adapter-node
  2. Build this website
  3. Create another svelte-kit website with the following configuration
import adapter from '@sveltejs/adapter-node'
import preprocess from 'svelte-preprocess'
import { handler } from 'sub-site/build/handler'

/** @type {import('@sveltejs/kit').Config} */
const config = {
  preprocess: preprocess(),
  kit: {
    adapter: adapter(),
    target: '#svelte',
    vite: {
      plugins: [
        {
          name: 'sub-site',
          configureServer({ middlewares }) {
            middlewares.use(handler) // Load the sub-site here
          },
        },
      ],
    },
  },
}

export default config

Logs

> Cannot redefine property: fetch
    at Function.defineProperties (<anonymous>)
    at __fetch_polyfill (file:///home/.../node_modules/.pnpm/@sveltejs+kit@1.0.0-next.229_svelte@3.46.1/node_modules/@sveltejs/kit/dist/install-fetch.js:6494:9)

System Info

SvelteKit v1.0.0-next.229
Adapter Node v1.0.0-next.61

Severity

annoyance

Additional Information

I'll submit a PR in a few minutes that fixes this issue, the solution is to add configurable: true to all definitions here:

export function __fetch_polyfill() {
Object.defineProperties(globalThis, {
fetch: {
enumerable: true,
value: fetch
},
Response: {
enumerable: true,
value: Response
},
Request: {
enumerable: true,
value: Request
},
Headers: {
enumerable: true,
value: Headers
}
});
}

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