Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapter-cfw: server-side fetch fails due to dependency on Node's util.types #1019

Closed
youngboy opened this issue Apr 14, 2021 · 4 comments · Fixed by #1066
Closed

adapter-cfw: server-side fetch fails due to dependency on Node's util.types #1019

youngboy opened this issue Apr 14, 2021 · 4 comments · Fixed by #1066

Comments

@youngboy
Copy link

youngboy commented Apr 14, 2021

Describe the bug

  1. Use adapter cloudflare workers,
  2. Find a route, make sure it turn prerender off, and it is doing fetch on server
  3. Open that route in browser directly, it won't work as expect.

(suspect it is about some thing about cfw's runtime have its fetch, and won't work node specific fetch bundle implementation ...)

Logs

website returning 500 code with $error.svelte route, which shows

Cannot read property 'message' of undefined

after fix $error.svelte (the above error is another issue, its hidding the real error)

p.URLSearchParams is not a constructor

TypeError: p.URLSearchParams is not a constructor

then i tried directly modify svelte's dist/ssr.js to avoid this, it keep showing different error xxx is not defined, like types.isAnyArrayBuffer, types.isBoxedPrimitive.

and that's all I dig so far,

To Reproduce

on kit repo

cd examples/svelte-kit-demo

// comment out  prerender, or mark it as false
vim src/routes/blog/index.svelte

pnpm build:cloudflare-workers

// change wrangler.toml's account_id with your cfw account it
vim wrangler.toml

wrangler  dev

open http://127.0.0.1:8787/blog

Expected behavior

example demo directly open /blog should displaying blogs list

Information about your SvelteKit Installation:

System:
OS: macOS 10.15.6
CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
Memory: 287.02 MB / 16.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 14.15.4 - ~/.nvm/versions/node/v14.15.4/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 7.6.2 - ~/.nvm/versions/node/v14.15.4/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Browsers:
Chrome: 89.0.4389.114
Firefox: 87.0
Safari: 13.1.2
npmPackages:
@sveltejs/kit: workspace:* => 1.0.0-next.74
svelte: ^3.35.0 => 3.35.0

Additional context
Add any other context about the problem here.

@youngboy youngboy changed the title adapter cfw seems not working in next.74 adapter cfw seems not working fetch on server Apr 14, 2021
@benmccann
Copy link
Member

benmccann commented Apr 14, 2021

p.URLSearchParams is not a constructor has been fixed but there's still another issue remaining:

util.types.isAnyArrayBuffer and util.types.isBoxedPrimitive are used by node-fetch and are not available in the CFW environment #1020 (comment)

@benmccann benmccann changed the title adapter cfw seems not working fetch on server adapter-cfw: server-side fetch fails due to dependency on Node's util.types Apr 14, 2021
@halfnelson
Copy link
Contributor

I thought this had been fixed by the cloudflare webpack shims, but I was mistaken. My original solution was a more heavy handed platform abstraction #223
But since then, the filesystem read code is no longer needed, and we really only need a proper isomorphic fetch

@youngboy
Copy link
Author

youngboy commented Apr 15, 2021

tried to use CFW version of fetch https://developers.cloudflare.com/workers/runtime-apis/fetch

export async function load({ fetch: kitFetch }) {
	const fn = typeof fetch === 'function' ? fetch : kitFetch
	const res = await fn('http://127.0.0.1:8787/blog.json');
	return {
		props: {
			posts: await res.json(),
		}
	};
}

but it is getting SyntaxError: Unexpected token e in JSON at position 0 error during res.json()..

PS: It is my first time trying out serverless, I find myself difficult to debug SSR mode in wrangler dev 😥 any suggestion ? thanks in advance...

@youngboy
Copy link
Author

tried to use CFW version of fetch https://developers.cloudflare.com/workers/runtime-apis/fetch

export async function load({ fetch: kitFetch }) {
	const fn = typeof fetch === 'function' ? fetch : kitFetch
	const res = await fn('http://127.0.0.1:8787/blog.json');
	return {
		props: {
			posts: await res.json(),
		}
	};
}

but it is getting SyntaxError: Unexpected token e in JSON at position 0 error during res.json()..

PS: It is my first time trying out serverless, I find myself difficult to debug SSR mode in wrangler dev 😥 any suggestion ? thanks in advance...

nvm..... change fetch method, Response class reference in kit/src/runtime/server/page/load_node.js will works fine in CFW adapter.

import { default as nodeFetch, Response as nodeResponse } from 'node-fetch';

const LoadFetch = typeof fetch === 'function' ? fetch : nodeFetch
const LoadResponse = typeof Response !== 'undefined' ? Response : nodeResponse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants