/ being changed to %2f on Vercel, breaking my data fetch from Builder
Unanswered
Morelet’s Crocodile posted this in #help-forum
Morelet’s CrocodileOP
Before fetching my data through Builder.get, I'm having to clean up the slug for the page I want to fetch because Vercel is changing the homepage URL from / to /index.
Now I'm running into a problem where "subpages" containing an additional / in their URL are 404ing because Vercel is changing the / to %2f before the data fetch.
Here are the relevant lines of code:
The output of that console.log on localhost is
The output on Vercel is
Any ideas?
Now I'm running into a problem where "subpages" containing an additional / in their URL are 404ing because Vercel is changing the / to %2f before the data fetch.
Here are the relevant lines of code:
let cleanedSlug = props?.params?.page?.join("/") || "";
if (cleanedSlug === "index") {
cleanedSlug = "";
}
const uri = "/" + decodeURI(cleanedSlug); // All that for what, Vercel?!
console.log('attempting to get data for: ' + uri)
const content = await builder
// Get the page content from Builder with the specified options
.get("page", {
userAttributes: {
// Use the page path specified in the URL to fetch the content
urlPath: uri,
},
})
// Convert the result to a promise
.toPromise();The output of that console.log on localhost is
attempting to get data for /foo/barThe output on Vercel is
attempting to get data for /foo%2fbarAny ideas?
17 Replies
is this nextjs?
if so, can you share more of the page
Morelet’s CrocodileOP
Here's the full page:
export const runtime = 'edge' // 'nodejs' (default) | 'edge'
import { builder } from "@builder.io/sdk";
import { RenderBuilderContent } from "../../components/builder";
import Header from "@components/Header"
import Footer from "@/components/Footer";
// Builder Public API Key set in .env file
builder.init(process.env.NEXT_PUBLIC_BUILDER_API_KEY);
export default async function Page(props) {
let cleanedSlug = props?.params?.page?.join("/") || "";
if (cleanedSlug === "index") {
cleanedSlug = "";
}
const uri = "/" + decodeURI(cleanedSlug); // All that for what, Vercel?!
console.log('attempting to get data for: ' + uri)
const content = await builder
// Get the page content from Builder with the specified options
.get("page", {
userAttributes: {
// Use the page path specified in the URL to fetch the content
urlPath: uri,
},
})
// Convert the result to a promise
.toPromise();
// Header data
let header = await builder.get("header").toPromise();
const headerNav = await builder.get("nav-menus", {
query: { id: header.data.navMenu.id }
}).toPromise();
// Replace the header nav menu with the data from Builder
header.data.navMenu = headerNav.data;
// Footer data
const menuIds =[
'7eda5235332643d9aff34bdb2adb37d8', '9dd6e1f2e0c747f7b703024f3a246a06', '6b3264e6a4204527a5b185d1b21af166'
];
const navMenus = await Promise.all(menuIds.map(id =>
builder.get('nav-menus', { query: { id } }).toPromise()
));
const footerData = await builder.get('footer').toPromise();
return (
<>
<Header data={header.data} />
{/* Render the Builder page */}
<RenderBuilderContent content={content} />
<Footer navMenus={navMenus} footerData={footerData} />
</>
);
}ahh nice, and you are using dynamic route im guessing?
Morelet’s CrocodileOP
Yes. [[...page]]/page.jsx
cool cool
@Morelet’s Crocodile Yes. [[...page]]/page.jsx
yeah so im superised about that because, your joining them by
/ but somehow it is being encoded....Morelet’s CrocodileOP
even the decodeURI function isn't doing anything
and if you just console log the params, is it a list or just one element ðŸ˜
Morelet’s CrocodileOP
props?.params?.page outputs foo,bar
wait so where is your issue then
sorry im a bit confused
Morelet’s CrocodileOP
I need to join that list to form "/foo/bar" to pass to the builder.get function to get the proper page content. The issue is that Vercel is encoding that second slash so the page is 404ing with no data returned from Builder.
Morelet’s CrocodileOP
Update. Removing the line for edge runtime seems to fix the issue. Is there a way to get around this to continue using edge runtime?
ahhh edge runtime be weird
uhh idk
might be worthwile opening issue (i haven't checked, but first make sure it doesn't already exist)