In the previous chapter we configured a custom domain for our serverless API. Now let’s do the same for our frontend React.js app.

Change indicator In the stacks/FrontendStack.ts add the following below the new StaticSite( line.

customDomain:
  app.stage === "prod"
    ? {
        domainName: "<YOUR DOMAIN>",
        domainAlias: "www.<YOUR DOMAIN>",
      }
    : undefined,

Just like the API case, we want to use our custom domain if we are deploying to the prod stage. This means that when we are using our app locally or deploying to any other stage, it won’t be using the custom domain.

Of course, you can change this if you’d like to use a custom domain for the other stages. You can use something like ${app.stage}.my-serverless-app.com. So for dev it’ll be dev.my-serverless-app.com. But we’ll leave this as an exercise for you.

The domainAlias prop is necessary because we want visitors of www.my-serverless-app.com to be redirected to the URL we want to use. It’s a good idea to support both the www. and root versions of our domain. You can switch these around so that the root domain redirects to the www. version as well.

You won’t need to set the domainAlias for the non-prod versions because we don’t need www. versions for those.

We need to use the custom domain URL of our API in our React app.

Change indicator Find the following line in stacks/FrontendStack.ts.

VITE_API_URL: api.url,

Change indicator And replace it with.

VITE_API_URL: api.customDomainUrl || api.url,

We also need to update the outputs of our frontend stack.

Change indicator Replace the stack.addOutputs call at the bottom of stacks/FrontendStack.ts with this.

stack.addOutputs({
  SiteUrl: site.customDomainUrl || site.url,
});

Here, we are returning the custom domain URL, if we have one. If not, then we return the auto-generated URL.

Deploy the App

Just like the previous chapter, we need to update these changes in prod.

Change indicator Run the following from your project root.

$ pnpm sst deploy --stage prod

At the end of the deploy process you should see something like this.

✓  Deployed:
   ...
   FrontendStack
   SiteUrl: https://my-serverless-app.com

And that’s it! Our React.js app is now deployed to prod under our own domain!

App update live screenshot

Commit the Changes

Change indicator Let’s commit our code so far and push it to GitHub.

$ git add .
$ git commit -m "Setting up custom domains"
$ git push

At this stage our full-stack serverless app is pretty much complete. In the next couple of optional sections we are going at how we can automate our deployments. We want to set it up so that when we git push our changes, our app should deploy automatically. We are also going to setup monitoring and error tracking.