Our website uses cookies.
Reject AllAllow all

This website stores cookies on your computer. The data is used to collect information about how you interact with our website and allow us to remember you. We use this information to improve and customize your browsing experience and for analytics and metrics about our visitors both on this website and other media.

Wildcard domain and SSR to the rescue!

Is that possible to achieve the same result with SSG?

Next.js 12.0.0 introduced a feature called Middlewares (beta), which lets us run code before a page request is completed. Based on the user's incoming request, we can modify the response by rewriting, redirecting, adding headers, or even streaming HTML. So it turns out, that we can pre-generate all the pages on build time and then rewrite incoming request to matching page based on its Host header.

For now we are happy with server-rendered pages, but we might want to use this cool feature in the future and switch to SSG. If you'd like to learn more about this implemenation, here is very cool example shared by vercel: https://github.com/vercel/examples/tree/main/edge-functions/hostname-rewrites

Mission completed

No more bugs and our app is live!

Do I regret time lost on configuring nginx and fighting with redirects? Not a bit! I’ve learned a lot about k8s, ingress, nginx, web requests, redirecting, proxing and wildcard domains. I wouldn’t have learned that much if we had jumped straight to server-side rendering.

It seems that the first solution is not always the best. Sometimes we have to take a step back and consider other available solutions to move forward! Thanks for reading! Keep learning!