Files
medusa-store/www/apps/docs/content/deployments/storefront/deploying-next-on-vercel.mdx
2023-11-28 15:52:29 +00:00

183 lines
8.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
description: 'Learn step-by-step.'
addHowToData: true
---
import DetailsList from '@site/src/components/DetailsList'
import CorsErrorSection from '../../troubleshooting/cors-issues.md'
# Deploy Next.js Starter Template on Vercel
In this document, youll learn how to deploy the Next.js Starter Template on Vercel.
## Prerequisites
### Medusa Components
It is assumed you already have installed the Next.js Starter Template locally. If not, please follow along with [this guide](../../starters/nextjs-medusa-starter.mdx) instead.
Its also assumed you already have the Medusa backend deployed, which the Next.js Starter Template interacts with. If not, you can check out one of the [deployment documentation related to the Medusa backend](../server/index.mdx).
### Storefront Preparations
If you're not using the [Serverless Modules](../../starters/nextjs-medusa-starter.mdx#using-serverless-modules) in your storefront, either delete the `src/app/api` directory or exclude it in `.gitignore`. Otherwise, the deployment will fail.
### Required Accounts
- [Vercel Account](https://vercel.com)
- [GitHub Account](https://github.com/): Only required if youre deploying through the Vercel website.
:::note
If you want to use another Git Provider, its possible to follow along with this guide, but youll have to perform the equivalent steps in your Git Provider.
:::
### Required Tools
- [Git CLI](../../development/backend/prepare-environment.mdx): Only required if youre deploying through the Vercel website.
---
## Step 1: Create GitHub Repository
:::note
This step is only required if youre deploying from the Vercel website. However, its highly recommended to connect your Vercel project to a Git repository for a better developer experience.
:::
Before you can deploy your Next.js Starter Template, you need to create a GitHub repository and push the code base to it. To do that:
1. On GitHub, click the plus icon at the top right, then click New Repository.
2. Youll then be redirected to a new page with a form. In the form, enter the Repository Name.
3. Scroll down and click Create repository.
### Push Code to GitHub Repository
The next step is to push the code to the GitHub repository you just created.
After creating the repository, youll be redirected to the repositorys page. On that page, you should see a URL that you can copy to connect your repository to a local directory.
Copy the link. Then, open your terminal in the directory that holds your Next.js Starter Template codebase and run the following commands:
```bash
git init
git remote add origin <GITHUB_URL>
```
Where `<GITHUB_URL>` is the URL you just copied.
Then, add, commit, and push the changes into the repository:
```bash
git add .
git commit -m "initial commit"
git push origin master
```
After pushing the changes, you can find the files in your GitHub repository.
---
## Step 2: Deploy to Vercel
This section covers how to deploy the storefront, either using the Vercel website or using Vercels CLI tool.
### Option 1: Using the Vercel Website
This section explains how to deploy the storefront using the Vercel website:
1. Open the [Vercel dashboard](https://vercel.com/dashboard) after logging in.
2. Click on the “Add New…” button next to the search bar.
3. Choose Project from the dropdown.
4. In the new page that opens, find the Git repository that holds your Next.js Starter Template and click on the Import button. If you havent connected your Vercel account to any Git provider, you must do that first.
5. In the Configure Project form:
1. Open the Environment Variables collapsible, and add the following environment variables:
- `NEXT_PUBLIC_MEDUSA_BACKEND_URL`: the URL to your deployed Medusa Backend.
- `NEXT_PUBLIC_BASE_URL`: The URL of your storefront. If you don't have a domain ready yet, you can add it later. However, you may face some errors in your storefront.
- `REVALIDATE_SECRET`: A secret used for revalidating data. Learn more in the [Next.js documentation](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation).
- If you're using Serverless modules, set the `POSTGRES_URL` environment variable to the connection URL of your PostgreSQL database, and set the `MEDUSA_FF_MEDUSA_V2=true` variable.
- You can set any other environment variables you're using at this step or later.
2. You can optionally edit the Project Name.
6. Once youre done, click on the “Deploy” button.
This will start the deployment of the storefront. Once its done, youll be redirected to the main dashboard of your new project.
:::note
At this point, when you visit the storefront, you will face errors related to Cross-Origin Resource Sharing (CORS) while using the storefront. Before you start using the storefront, follow along the [Configure CORS on the Medusa Backend](#step-3-configure-cors-on-the-medusa-backend) section.
:::
### Option 2: Using Vercels CLI Tool
This section explains how to deploy the storefront using the Vercel CLI tool. You should have the CLI tool installed first, as explained in [Vercels documentation](https://vercel.com/docs/cli).
In the directory holding your storefront, run the following command to deploy your storefront:
```bash
vercel --build-env NEXT_PUBLIC_MEDUSA_BACKEND_URL=<YOUR_BACKEND_URL>
vercel --build-env NEXT_PUBLIC_BASE_URL=<YOUR_BASE_URL>
vercel --build-env REVALIDATE_SECRET=<YOUR_REVALIDATION_SECRET>
```
Where:
- `<YOUR_BACKEND_URL>`: the URL of your deployed Medusa backend.
- `<YOUR_BASE_URL>`: The URL of your storefront. If you don't have a domain ready yet, you can add it later. However, you may face some errors in your storefront.
- `<YOUR_REVALIDATION_SECRET>`: A secret used for revalidating data. Learn more in the [Next.js documentation](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation).
In addition:
- If you're using Serverless modules, set the `POSTGRES_URL` environment variable to the connection URL of your PostgreSQL database, and set the `MEDUSA_FF_MEDUSA_V2=true` variable.
- You can set any other environment variables you're using at this step or later.
Youll then be asked to log in if you havent already, and to choose the scope to deploy your project to. You can also decide to link the storefront to an existing project, or change the projects name.
When asked `In which directory is your code located?`, keep the default `./` and just press Enter.
The project setup will then start. When asked if you want to modify the settings, answer `N` to keep the default settings.
It will take a couple of minutes for the deployment to finish. The link to the storefront will be shown in the final output of the command.
:::note
At this point, when you visit the storefront, you will face errors related to Cross-Origin Resource Sharing (CORS) while using the storefront. Before you start using the storefront, follow along the [Configure CORS on the Medusa Backend](#step-3-configure-cors-on-the-medusa-backend) section.
:::
---
## Step 3: Configure CORS on the Medusa Backend
To send requests to the Medusa backend from the Next.js Starter Template, you must set the `STORE_CORS` environment variable on your backend to the Next.js Starter Templates URL.
:::tip
If you want to set a custom domain to your Next.js Starter Template website on Vercel, make sure to do it before this step. You can refer to this guide on [Vercels documentation](https://vercel.com/docs/concepts/projects/domains/add-a-domain).
:::
On your Medusa backend, add the following environment variable:
```bash
STORE_CORS=<STOREFRONT_URL>
```
Where `<STOREFRONT_URL>` is the URL of your Next.js Starter Template that you just deployed.
Then, restart your Medusa backend. Once the backend is running again, you can use your Next.js Starter Template.
---
## Troubleshooting
<DetailsList
sections={[
{
title: 'CORS Error',
content: <CorsErrorSection />
},
]}
/>