FunAndFriendly
FunAndFriendly
NNuxt
Created by FunAndFriendly on 3/6/2025 in #❓・help
How to reroute a request to a different template: GET /hello -> render(/foo/hello.vue)
Hi, when a request comes into the server, I want to check the hostname and decide which page to render. How can I do that? I've looked at nuxt-multi-tenant module and it's possible, but that module uses dynamic routes. I have static routes.
Seems like this should be an easy win, but when I try to modify event.page in defineEventHandler() it's read-only. 😦 Howto? Thanks!
6 replies
NNuxt
Created by FunAndFriendly on 3/2/2025 in #❓・help
How to dynamically render a different page for a request. /pages/some/thing.vue or other/thing.vue
Hi, I'm trying to make a middleware like this - but setRequestURL() isn't a real thing and event.path is read-only. Requirements:
1. no client-side redirects. I want to change the response for /some/thing 2. I need to dynamically decide to make the switch or not.
// If the request is for /some/thing, return the render for /other/thing
export default defineEventHandler((event) => {
const { pathname } = getRequestURL(event);
if (pathname === '/some/thing' && random() > .5) {
setRequestURL(event, new URL('/other/thing', getRequestURL(event).origin));
}
});
// If the request is for /some/thing, return the render for /other/thing
export default defineEventHandler((event) => {
const { pathname } = getRequestURL(event);
if (pathname === '/some/thing' && random() > .5) {
setRequestURL(event, new URL('/other/thing', getRequestURL(event).origin));
}
});
I want to cache dynamic content - there are only two versions of GET /some/thing - /pages/some/thing.vue and /pages/other/thing.vue How to do this? Thank you!
6 replies