Subsequent.js Wildcard Subdomains

No Comments

A “wildcard”? What on the earth? Nice query, all these area stem from Wildcard DNS Information which appear like this:

*.instance. 3600 TXT “Wild! You’ve gotten discovered a wildcard.”

When used, this DNS file will trigger any subdomain that matches with the wildcard to carry a TXT worth of: “Wild! You’ve gotten discovered a wildcard.”

For instance, if this was set on the area, and would each return the above TXT worth. The identical precept might be utilized to CNAME & A data as effectively.

Wild Use Instances For Wildcards

Wildcards can be utilized for a variety of issues. For now, let’s concentrate on the place they are often utilized together with Subsequent.js:

Offering Hosted Companies
The commonest use of wildcard domains is to supply customers of hosted companies their very own area with a singular subdomain. For instance, if I used to be constructing a platform for eating places to host digital ordering platforms with the area, I’d be capable to provide Dom’s Pizzeria and Magical Prata the area The good thing about that is that it offers every of those institutions their enclosed area which they’ll customise and construct out. This area can act as its personal web site — not tied to something.
Internet hosting Content material And Private Portfolios
Wildcards may also be used as an area for internet hosting content material in portfolios, giving a way of individuality to those portfolios, an instance of this might be how Medium supplies subdomains for the authors.
Wilder Extra Artistic Use Instances
You may’t outline these use circumstances, however there are a lot of artistic use circumstances of those types of domains. For instance, later on this article, we’ll be creating an internet toy that flips a webpage the other way up making it readable for Australians.

The Caveats Of Wildcards With Subsequent.js

Sigh. Sadly, utilizing wildcards isn’t good there are a few drawbacks:

No Extra Static-Web site Technology (and ISR)
Sadly, there aren’t any particular techniques to supply customized statically generated pages for various wildcard subdomains like you might with dynamic routing for instance (the place you will have [slug].js information).
Difficulties with Improvement
When creating domestically, it may be a ache to simulate wildcard domains and we’ll be referring to this a good bit in a while on this article however it’s one thing necessary to bear in mind.
Restricted Deployment Platforms
Vercel helps Wildcard Domains, nonetheless, different Jamstack oriented platforms don’t all assist wildcard domains. For instance, Netlify limits the characteristic to a choose group of customers on the Professional plan.

Constructing With Wildcards

With all this discuss, let’s take a look at constructing with these domains. We’ll be specializing in three locations the place you may get the wildcard:

Server Aspect In getServerSideProps,
Shopper Aspect With useEffect,
Server Aspect On API Routes And Edge Capabilities.

Server Aspect In getServerSideProps

That is essentially the most commonplace during which you will want to extract the wildcard, you should utilize this on pages the place you have to render fully totally different content material for various wildcards. As mentioned above, this cannot be achieved via static web site era so we should do it on server-side rendered pages.

The getServerSideProps is handed a context object, on this object you possibly can entry the HTTP request object utilizing context.req. On this request object, you possibly can entry the hostname at, which can return a string comparable to We will cut up the string into an array throughout every interval after which entry the primary merchandise in mentioned array. In code, that appears like this:

export async perform getServerSideProps(context) {
let wildcard = up(“.”)[0];
wildcard =
wildcard != “yourdomain”
? course of.env.NODE_ENV != “improvement”
? wildcard
: course of.env.TEST_WILDCARD
: “dwelling”;
return { props: { wildcard } };

As you possibly can see on this piece of code, we do an additional set processing on the wildcard if it’s the bottom area we set the wildcard to dwelling (if taking person enter, this can be a case you will want to deal with) and if we’re testing on localhost we will check out different wildcards. In our default export perform, which renders our web page we will use a change assertion to deal with the wildcards:

export default perform App(props) {
change(props.wildcard) {
case “dwelling”:
return <div>Welcome to the house web page!</div>;
return <div>The wild card is: {props.wildcard}.</div>;

Shopper Aspect With useEffect

For those who solely wish to make small modifications to every web page on a special wildcard, you possibly can keep away from utilizing server-side rendering through the use of the useEffect hook on the client-side. This strategy will likely be slightly just like how we did it in getServerSideProps, besides we will likely be counting on window.location.hostname. Utilizing window implies that the preliminary server render received’t be capable to entry the knowledge, so we should wrap it inside a useEffect hook that runs on the client-side. Right here’s how that code appears like:

// useEffect and useState have to be imported from ‘react’

const [wildcard, setWildcard] = useState(“”)
useEffect(() => {
setWildcard(window.location.hostname.cut up(“.”)[0])
}, [])

This strategy, nonetheless, is way from good as there’s a delay between the web page’s first render and the wildcard being obtainable. Subsequently, if you’re making drastic adjustments based mostly on the wildcard then the adjustments will likely be jarring on your person. This may occasionally additionally damage your cumulative structure shift measure in your internet vitals. With this in thoughts, I extremely suggest limiting your use of this strategy to diversifications that might be off-sight from the viewer on the preliminary load. An instance can be a branded footer, on a technical documentation web page. It’s, in fact, nonetheless helpful to know.

Server Aspect On API Routes And Edge Capabilities

API routes are one other space the place you might wish to entry a wildcard from. Happily, the identical request object we mentioned within the above part on getServerSideProps can also be obtainable when utilizing a Node.js API route with Subsequent.js. We will entry it like this:

export default (req, res) => {
let wildcard = up(“.”)[0];
wildcard =
wildcard != “yourdomain”
? course of.env.NODE_ENV != “improvement”
? wildcard
: course of.env.TEST_WILDCARD
: “dwelling”;
res.json({ wildcard: wildcard })

Following this, we will then take sure actions comparable to fetching totally different information out of your database relying on the wildcard and return that from the API.

This identical logic might be utilized to Subsequent.js’ new Edge Capabilities / Middleware. This lets you use wildcards in multiple route with out duplicating code in addition to dashing up the processing as code execution occurs on the sting. While the performance remains to be in beta, it’s actually one thing to keep watch over.

// _middleware.js
export perform middleware(req) {
let wildcard = req.headers.get(“host”).cut up(“.”)[0];
wildcard =
wildcard != “yourdomain”
? course of.env.NODE_ENV != “improvement”
? wildcard
: course of.env.TEST_WILDCARD
: “dwelling”;
console.log(course of.env.TEST_WILDCARD);
return new Response(JSON.stringify({ wildcard: wildcard }), {
standing: 200,
headers: { “Content material-Kind”: “utility/json” },

The 🦘Aussie-izer

Now that we’ve explored the idea of this technique, let’s discover how we put it into observe. On this part, we’ll be taking this strategy to construct a challenge that flips web sites the other way up (effectively, web sites which are utilizing a .com area and aren’t subdomains) referred to as the 🦘Aussie-izer.

To get began, we’re going to wish to run yarn init after which yarn add subsequent react react-dom, and end up by including these normal scripts to our bundle.json:

“scripts”: {
“dev”: “subsequent dev”,
“construct”: “subsequent construct”,
“begin”: “subsequent begin”,
“lint”: “subsequent lint”

As quickly as we’ve received a typical Subsequent.js challenge arrange, we’re going to wish to create the one code file we’ll want for this challenge: pages/index.js.

First, we’ll wish to add the getServerSideProps perform during which we’ll extract the wildcard (as I’ll be internet hosting this at that’s what I’ll be evaluating to dwelling as:

export async perform getServerSideProps(context) {
let wildcard = up(“.”)[0];
wildcard =
wildcard != “aussieizer”
? wildcard != “localhost:3000”
? wildcard
: course of.env.TEST_WILDCARD
: “dwelling”;
return { props: { wildcard } };

We’ll then be utilizing that wildcard, to render an iFrame to fill that web page (by flipping iFrame over to create the impact), with our src being set to {`https://${props.wildcard}.com`}. We’ll use a change case, as we mentioned above, to render a small helper web page in the event that they go to the house web page:

export default perform App(props) {
change (props.wildcard) {
case “dwelling”:
return (
Welcome to the Aussie-izer! This solely works for .com domains. For those who
wish to Aussie-ize{” “}
<a href=””></a> go to{” “}
<a href=””>
return (
rework: “rotate(180deg)”,
border: “none”,
peak: “100vh”,
width: “100%”,
overflow: “hidden”,

And we’re able to go! The reside model is accessible at and the supply code might be discovered at

Internet hosting/Deployment

For those who’re internet hosting on a customized server, wildcard domains will likely be a breeze to arrange via DNS. Nevertheless, an awesome half about utilizing Jamstack is with the ability to host on companies comparable to Vercel or Netlify; these companies have their very own area administration techniques.


Vercel helps wildcard domains out of the field — for all accounts. To make use of them, first go to the Domains part of your deployment’s Settings tab. Subsequent, you’ll wish to enter your area through the use of a * to suggest the wildcard.

For the above instance, I entered:


You probably will even wish to add your root area (, in my case) to have the ability to present a homepage or some directions, nonetheless, that may be a separate codebase.


Netlify limits their wildcards characteristic to Professional accounts; you probably have a Professional account, you will want to e-mail their assist employees for them to then allow the choice in your account. It’s going to present up within the area settings web page as soon as enabled.


Render additionally gives wildcard domains to all customers. Merely enter a site with a * (signifying your wildcard) within the Add Customized Area enter on the customized domains part of your web site’s settings web page which can allow the wildcard. Please be aware that Render would require you so as to add extra data to your DNS in order that they’ll subject a LetsEncrypt SSL certificates (precise directions will likely be proven to you once you enter your wildcard area).

That’s It!

Wildcard domains typically go beneath the radar — I hope you loved exploring them with me. Thanks!

Additionally: FYI Australians don’t truly see the other way up.

Additional Studying On Smashing Journal

The Evolution Of Jamstack,” Mathias Biilmann
World vs. Native Styling In Subsequent.js,” Alexander Dubovoy
Breaking Down Cumbersome Builds With Netlify And Subsequent.js,” Atila Fassina
What Is Subsequent.js?,” Smashing Podcast episode with Guillermo Rauch
How Does Netlify Dogfood The Jamstack?,” Smashing Podcast episode with Leslie Cohn-Wein

    About Marketing Solution Australia

    We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

    Request a free quote

    We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

    Subscribe to our newsletter!

    More from our blog

    See all posts

    Leave a Comment