Skip to content

Support Vercel's skew protection #10947

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

Closed
Nick-Mazuk opened this issue Oct 28, 2023 · 2 comments · Fixed by #11987
Closed

Support Vercel's skew protection #10947

Nick-Mazuk opened this issue Oct 28, 2023 · 2 comments · Fixed by #11987
Labels
pkg:adapter-vercel Pertaining to the Vercel adapter

Comments

@Nick-Mazuk
Copy link
Contributor

Describe the problem

Vercel's skew protection is in beta, but currently only NextJS supports it. Adding this to Sveltekit eliminates entire classes of problems/bugs.

See also:

Describe the proposed solution

https://vercel.com/docs/deployments/skew-protection

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

@eltigerchino
Copy link
Member

eltigerchino commented Oct 28, 2023

I'm curious if it would be as simple as patching the window.fetch object of start.js to pass the deployment id during adapter-vercel's build (seems a bit hacky though). https://vercel.com/docs/deployments/skew-protection#additional-request-types I don't really understand how it works. Seems to be just passing the 'X-Deployment-Id' header for any client requests to the server?

/**
 *
 * @param {import('@sveltejs/kit').Builder} builder
 * @param {string} static_dir
 */
function enable_skew_protection(builder, static_dir) {
	const app_path = builder.getAppPath();
	const client_entry_path = path.join(static_dir, app_path, 'immutable/entry');

	const start_file_name = fs
		.readdirSync(client_entry_path)
		.filter((filename) => filename.startsWith('start'))[0];
	const start_file_path = path.join(client_entry_path, start_file_name);
	const start_file = fs.readFileSync(start_file_path);

	const skew_protection_patch = `const native_fetch=window.fetch;window.fetch=(e,t)=>(t.headers["X-Deployment-Id"]=${process.env.NEXT_DEPLOYMENT_ID},native_fetch(e,t));`;

	write(start_file_path, skew_protection_patch + start_file);
}

@jean343
Copy link

jean343 commented Dec 7, 2023

If this helps, it was done in vite with:

export default defineConfig({
  experimental: {
    renderBuiltUrl(filename) {
      return `${filename}?dpl=${process.env.VERCEL_DEPLOYMENT_ID}`;
    },
  },
});

@eltigerchino eltigerchino added the pkg:adapter-vercel Pertaining to the Vercel adapter label Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:adapter-vercel Pertaining to the Vercel adapter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants