Describe your question/issue in detail
I'm currently implementing a level 2 deep resolvers like this.
{
type: 'page',
resolvers: {
segment: 'route_segment',
parent: 'route_segment.parent',
},
path: '/:parent?/:segment?/:uid',
},
the route_segment is a custom type with a parent (optional) that can lead to another route_segment.
Impacted feature
Route resolvers.
What steps have you taken to resolve this issue already?
Errors
Everythink works fine. I can acces my page of 2 level deep but the link if the page is weird because the url is broken. It seems to return a weird id the of grandfather custom type instead of the uid. I said weird id because I have the ID plus some other string.
http://localhost:3000/>&<00018aD7-8BMAACAAQU6y%%/isolation/isolation-combles-perdus
Your Role
Developper
Hosting provider
Package.json file
Steps to reproduce
Sorry but the url generate in the page is correct. It's the one in the link that is wrong.
The problem happen because the link is from a custom type Menu that contain slice with a content relationship with other slice that contain links.
const layout = await client.getSingle('layout', {
graphQuery: `{
layout {
...layoutFields
slices {
...on menu_item {
variation {
...on default {
primary {
...primaryFields
}
}
...on withSubMenu {
primary {
label
sub_menu {
title
footer_link
footer_image
slices {
...on sub_menu_item {
variation {
...on card {
primary {
...primaryFields
}
}
...on groupListWithHeader {
primary {
...primaryFields
}
}
}
}
}
}
}
}
}
}
}
}
}
`,
});
To everyone who want a dirty solution.
The following function iterate over all nested object to check if there is a link. Then fetch the original one with a getById and replace the broken url with the new one.
async function resolveLinksRecursively(obj, client) {
if (Array.isArray(obj)) {
return Promise.all(obj.map(item => resolveLinksRecursively(item, client)));
} else if (obj && typeof obj === 'object') {
// If this is a Prismic document link
if (
obj.link_type === 'Document' &&
obj.id &&
typeof obj.url === 'string'
) {
try {
const doc = await client.getByID(obj.id);
return {
...obj,
url: doc.url,
};
} catch {
// If fetch fails, return the original link
return obj;
}
}
// Otherwise, recursively resolve all properties
const entries = await Promise.all(
Object.entries(obj).map(async ([key, value]) => [key, await resolveLinksRecursively(value, client)])
);
return Object.fromEntries(entries);
}
// Primitive value
return obj;
}
2 Likes