N
Nuxt2w ago
holloway

Wildcard routes that vary by file extension that Nuxt interprets as overlapping routes

I'm replacing a site with two server routes at /rfc/rfcN.json and /rfc/rfcN.txt and an HTML route at /rfc/rfcN/ where the N is a number. Making files in server/routes/rfc/ named [id].txt.ts and [id].json.ts doesn't result in Nuxt serving each route distinctly. Should I use a single route file that handles them all (if so how do I handle server and vue routes?), or middleware to redirect, or what?
3 Replies
kapa.ai
kapa.ai2w ago
Hi @holloway. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To address your question about handling wildcard routes with different file extensions in Nuxt, I'll provide some guidance based on the available information in the knowledge sources. The Nuxt file-system routing typically creates routes based on the structure of the pages/ directory. However, for server routes, we need to look at the server/ directory structure. Unfortunately, the knowledge sources don't provide specific information about handling multiple file extensions for the same dynamic route in the server directory. However, we can approach this problem using a combination of techniques: 1. Use a catch-all route: You can create a catch-all route in your server/api/ directory to handle all requests for RFCs. This approach is mentioned in the Nuxt documentation on server directory structure. Create a file named server/api/rfc/[...].ts:
export default defineEventHandler((event) => {
const path = event.context.params._
const [id, extension] = path.split('.')

if (!extension) {
// Handle HTML route
return `HTML content for RFC ${id}`
} else if (extension === 'json') {
// Handle JSON route
return { rfc: id, type: 'json' }
} else if (extension === 'txt') {
// Handle TXT route
return `Text content for RFC ${id}`
} else {
throw createError({
statusCode: 404,
statusMessage: 'Not Found'
})
}
})

export default defineEventHandler((event) => {
const path = event.context.params._
const [id, extension] = path.split('.')

if (!extension) {
// Handle HTML route
return `HTML content for RFC ${id}`
} else if (extension === 'json') {
// Handle JSON route
return { rfc: id, type: 'json' }
} else if (extension === 'txt') {
// Handle TXT route
return `Text content for RFC ${id}`
} else {
throw createError({
statusCode: 404,
statusMessage: 'Not Found'
})
}
})

This approach allows you to handle all three types of routes (HTML, JSON, and TXT) in a single file. 2. Use middleware for redirection: If you prefer to keep your routes separate, you could use middleware to redirect requests to the appropriate handler. This can be done by creating a middleware file in the middleware/ directory. However, this approach might be more complex and less efficient than using a catch-all route. 3. Custom server handler: For more complex routing logic, you might need to create a custom server handler. However, this is an advanced topic and not covered in the provided knowledge sources. Given the limitations of the information available in the knowledge sources, the catch-all route approach seems to be the most straightforward solution to your problem. It allows you to handle both server routes (/rfc/rfcN.json and /rfc/rfcN.txt) and the HTML route (/rfc/rfcN/) in a single file. If you need more specific information or if this solution doesn't fully address your needs, you might want to consult the full Nuxt documentation or seek help from the Nuxt community forums. Nuxt Server Directory Structure ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@678484290828238868> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
holloway
hollowayOP2w ago
Well if I have an HTML route too then how would I render a Vue page? And if I manually rendered it in a server route would I be opting out of Nuxt features like SWR?

Did you find this page helpful?