Build output API using Edge runtime
Unanswered
Philippine Crocodile posted this in #help-forum
Philippine CrocodileOP
How to get the slug in function using Edge runtime?
20 Replies
Philippine CrocodileOP
In my index file of
index.func dir I have this function that gets executed when the /* route is visited. The route handles the slug using the prerender config. So in my function, I am trying to access the slug using the request parameter but the request object is emptyexport default async function index(request) {
console.log("req", request);
return Response.json({
message: "A Ok!",
});
}The prerender config is working fine and not throwing any errors or 404s, I just need to access the slug passed. Is it not possible when using Edge runtime or I am missing something?
@Philippine Crocodile In my index file of `index.func` dir I have this function that gets executed when the `/*` route is visited. The route handles the slug using the prerender config. So in my function, I am trying to access the slug using the request parameter but the request object is empty
javascript
export default async function index(request) {
console.log("req", request);
return Response.json({
message: "A Ok!",
});
}
did you mean this?
export const runtime = "edge";
// app/items/[slug]/route.ts
export async function GET(
request: Request,
{ params }: { params: { slug: string } }
) {
const slug = params.slug; // 'a', 'b', or 'c'
}Philippine CrocodileOP
I am not using NextJS
I am creating it using Build output API
@Philippine Crocodile I am not using NextJS
you want to get the pathname?
new URL(request.url).pathnamePhilippine CrocodileOP
Yes but request is empty object
Philippine CrocodileOP
If you console log the request param it will be an empty object
@Philippine Crocodile Check this out https://github.com/vercel/examples/tree/main/build-output-api/edge-functions
isn't the pathname should be the file path?
Philippine CrocodileOP
Not exactly in this case
Philippine CrocodileOP
So the config.json file inside the .vercel directory has the filehandling config
{
"version": 3,
"routes": [
{
"handle": "filesystem"
},
{
"src": "/(?<slug>[^/]*)",
"dest": "/index?slug=$slug"
}
]
}ah so its on the searchParams
Philippine CrocodileOP
this way anything comes after / will become the slug
I deployed in vercel and tested if this works and it worked as expected
but the only problem is that I cannot access the slug and the request or the event objects are empty
Could it be because of the Edge runtime?
@Ray ah so its on the searchParams
Philippine CrocodileOP
Exactly, I just want to get the serachParam some way