-
Notifications
You must be signed in to change notification settings - Fork 10
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
[Proposal] Abstracting Next.js away #23
Comments
Definitely! That's a great idea. Do the other frameworks use the same edge runtime as Next.js? If so then adding support for them shouldn't take too much work because the edge-specific computations are all encapsulated in util.ts. Let me know what you're thinking and how I can help. |
Awesome, thank you! You could pretty much run all these frameworks (SvelteKit, Remix, Astro) at the edge using Vercel or Cloudflare Workers - and this update would unlock using this package for all of these. I believe we could replace all the Then - it's a matter of exporting the various implementations - although it's likely that it's only Next.js that needs the current I am not sure how we could export the implementations, but I think exporting framework-specific packages could be an idea (maybe we can create NPM modules in this repository to export/publish them so you don't need to create different repositories). So for Next.js we'd export Unfortunately, this would require a major version bump! |
Here's a draft PR that adds sveltekit support via a new handle function: #30 The way you use it is by adding a server hook like this: // src/hooks.server.ts
import type { Handle } from '@sveltejs/kit';
import { createHandle } from 'edge-csrf/sveltekit';
// initalize csrf protection handle
export const handle: Handle = createHandle({
cookie: {
secure: process.env.NODE_ENV === 'production',
},
}); Here are full working examples that implement form processing via server actions in Vercel's edge runtime and Cloudflare Pages: Let me know what you think. I'm new to SveleteKit so any feedback would be very much appreciated. |
Quick update - I switched from a default export to a named export ( And here's a proposal for the nextjs API: // middleware.ts
import { createHandler } from 'edge-csrf/nextjs';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
const csrfHandler = createHandler({
cookie: {
secure: process.env.NODE_ENV === 'production',
},
});
export async function middleware(request: NextRequest) {
const response = NextResponse.next();
// csrf protection
const csrfError = await csrfHandler(request, response);
// check result
if (csrfError) {
return new NextResponse('invalid csrf token', { status: 403 });
}
return response;
} |
That looks great! The only concern is that the dependencies will require (or download) Next.js and Sveltekit - while a multi-package (which definitely adds dev overhead) would avoid. The ideal scenario (in my mind):
This could be done using npm/pnpm workspaces from within this repository. I've noticed you cut a v2 so for for thought about this! |
Here's a release candidate for edge-csrf with support for Next.js and SvelteKit: To use it locally you can install npm install [email protected] Here are some things I'd like some feedback on:
|
Thanks for the feedback! Looks like I missed your message by 2 minutes. I've also been leaning towards releasing the code as separate packages:
Doing so would make listing the dependencies easier and it would also solve some Let me know what you think! |
Quick update - here's a version that splits the integrations into separate packages using a pnpm workspace: Let me know what you think. |
2.0.0 is live! Thanks so much for your help. Would you be interested in adding Remix and Astro integrations? |
Fantastic, thank you so much! Looks great and exactly how I thought it'd work. I believe Remix does not yet have middleware (so users need to pass it from a root loader down to the page and protect actions manually), but the utilities from the core will make it still simple. Maybe I can write a guide for the time being |
Hi!
I use this package with Next.js - and would love to use it with other frameworks - so they can run well in edge runtimes.
I'd love to know if there would be any interest in abstracting this package away from Next.js and perhaps exporting a framework-less version - while using a different library (perhaps in an npm workspace) for exporting framework-specific implementations (Next.js, Remix, Sveltekit, etc.).
Happy to contribute if so!
The text was updated successfully, but these errors were encountered: