Make internal redirect inside onBeforeRender #721
-
Hello, There is an example from the documentation of how I can do it with responding redirect status code to the client. But there are problems with this approach:
It feels that I need something like this: // pages/data/index.page.server.ts
export async function onBeforeRender(pageContext: PageContext) {
const data = await loadData();
if (data.forbidden) {
// here I need to call smth to start a new render of a different page that I specify.
// the forbidden page is completely different and has no common with "data" page
// I don't want to call throw RenderErrorPage() because my "error" page is specific to that concrete route,
// but RenderErrorPage will render _error.page.vue which is common for all routes
return renderPage('/_internal/data-forbidden');
}
} For now, I ended up with a slightly modified approach from documentation by executing app.get('*', async (req, res, next) => {
....
let pageContext = await renderPage(pageContextInit);
if (pageContext.redirectTo) {
pageContext = await renderPage({
urlOriginal: pageContext.redirectTo,
data: pageContext.redirectToData
});
}
....
}); But it feels wrong to me... Could somebody give me an advice on how it would be better to do this? P.S. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
How about making your
Thanks for your kind words, I appreciate it. |
Beta Was this translation helpful? Give feedback.
How about making your
Page
defined inpages/data/index.page.ts
render the error page while setting a new proppageContext.isNotAuthorized
that you can use atrenderPage()
to show the correct status code?Thanks for your kind words, I appreciate it.