Building a multi-tenant app with Next.js subdomain is a great way to create a scalable and flexible solution for your users.
You can use the `getServerSideProps` method to fetch data from a database or API and render it on the client-side.
Having a multi-tenant app allows you to serve multiple customers or tenants from a single codebase.
In Next.js, you can create a subdomain for each tenant by using the `next.config.js` file to configure the routing.
Domain Configuration
If you want to serve your localized content based on different domains, you can provide a list of mappings between domains and locales via the domains setting. For example, you can map us.example.com to the en locale, ca.example.com to the en locale, and ca.example.com to the fr locale.
You can optionally remove the locale prefix in pathnames by changing the localePrefix setting. This can be helpful if you have unique domains per locale.
If no domain matches, the middleware will fall back to the defaultLocale. For example, on localhost, the middleware will use the default locale.
To test your domain setup locally, you can conditionally adapt the domains to refer to hosts that are available locally. This can be done by building the app for each domain separately and injecting diverging routing configuration via an environment variable.
Here's an example of how to do this:
Keep in mind that this configuration requires some tradeoffs, such as not being able to render pages statically due to the need to read the domain at runtime.
Test and Deploy
Testing your Next.js app is crucial to ensure it's working as expected. You can run npm run dev to test the app.
Navigate to localhost:3000 to verify the app is running correctly. Make sure you're running on localhost:3000 because that's what you put in the middleware.ts file.
You can test the subdomain routing by visiting test2.localhost:3000. The content should change to This is Site 2.
Deploying your application is the next step. You can deploy your app with vercel --prod.
Multi-Tenancy Setup
Setting up multi-tenancy on a Next.js subdomain requires a clear understanding of how to configure routing and API routes.
To begin, you'll need to create a new file in the `pages` directory for each subdomain, using the `next.config.js` file to specify the routing configuration.
In the `next.config.js` file, you can use the `middleware` option to set up a catch-all route for unknown subdomains, redirecting them to a default page or error handling route.
You can use the `getServerSideProps` method in your page components to fetch data from an API, which can be used to populate the subdomain-specific content.
For example, you can use the `req.headers.host` property to extract the subdomain from the request headers, and then use this value to fetch the relevant data from the API.
By setting up multi-tenancy in this way, you can create a scalable and flexible architecture for your Next.js subdomain application.
Using a single API route for all subdomains can simplify the development process, but may require additional logic to handle subdomain-specific data.
You can use a single API route for all subdomains, and then use the `req.headers.host` property to determine which subdomain is making the request.
This approach can be particularly useful when working with a large number of subdomains, as it reduces the overhead of maintaining multiple API routes.
Sources
- https://developers.cloudflare.com/pages/configuration/custom-domains/
- https://next-intl-docs.vercel.app/docs/routing/middleware
- https://next-intl-docs.vercel.app/docs/routing
- https://dev.to/iskurbanov/step-by-step-multi-tenant-app-with-nextjs-2mbc
- https://blog.alexcloudstar.com/setting-up-nextjs-13-with-auth0-and-sub-domains-a-guide-to-multi-tenancy-web-apps
Featured Images: pexels.com