anymaniax
anymaniax
CDCloudflare Developers
Created by anymaniax on 7/10/2024 in #workers-help
Worker replaced by Cloudflare snippets
Ok thanks @Cyb3r-Jok3!
3 replies
CDCloudflare Developers
Created by anymaniax on 7/19/2023 in #pages-help
Middleware forward headers to other functions
I feel stupid right now lul Thanks for your time 😄
13 replies
CDCloudflare Developers
Created by anymaniax on 7/19/2023 in #pages-help
Middleware forward headers to other functions
I didn't test it but I did a little reproduction here https://github.com/anymaniax/cloudflare-middleware-forward. I don't know how to run wrangler dev locally when I tried to do npx wrangler dev ./.vercel/output/static/_worker.js/index.js --compatibility-flags nodejs_compat I have an error
13 replies
CDCloudflare Developers
Created by anymaniax on 7/19/2023 in #pages-help
Middleware forward headers to other functions
I maybe don't understand something. But I tried again and it doesn't work the same way in local and when I deploy. If you check here https://d0ba92e2.altissia-apps.pages.dev/login?interfaceLg=fr_FR I have middleware that set the header x-interface-lg and I try to access that header in the layout just after passing the middleware to set the good interface language but like this, the header is not forwarded
13 replies
CDCloudflare Developers
Created by anymaniax on 7/19/2023 in #pages-help
Middleware forward headers to other functions
Indeed and the header is not forwarded to the other functions when I deploy with @cloudflare/next-on-pages. Otherwise, locally it's works perfectly
13 replies
CDCloudflare Developers
Created by anymaniax on 7/19/2023 in #pages-help
Middleware forward headers to other functions
Found a solution to my problem
export async function middleware(request: NextRequest) {
const hasHref = request.headers.get('x-href');

if (!hasHref) {
const newRequest = new Request(request.url, request);

newRequest.headers.set('x-href', request.nextUrl.href);

const response = await fetch(newRequest);

return new Response(response.body, response);
}

return NextResponse.next();
}
export async function middleware(request: NextRequest) {
const hasHref = request.headers.get('x-href');

if (!hasHref) {
const newRequest = new Request(request.url, request);

newRequest.headers.set('x-href', request.nextUrl.href);

const response = await fetch(newRequest);

return new Response(response.body, response);
}

return NextResponse.next();
}
13 replies
CDCloudflare Developers
Created by anymaniax on 7/19/2023 in #pages-help
Middleware forward headers to other functions
Currently trying something like this but I have a 500 when I deploy
export async function middleware(request: NextRequest) {
request.headers.set('x-href', request.nextUrl.href);

return fetch(request);
}
export async function middleware(request: NextRequest) {
request.headers.set('x-href', request.nextUrl.href);

return fetch(request);
}
13 replies