Let's deploy a static Next.js app to an S3 Bucket. I will use Yandex Object Storage (an S3-compatible service by Yandex). But it could be any S3 service and any "static" web app (Vite React, bare HTML, etc.); the process will be very similar.
Create Next.js App
bun create next-app
bun create next-app
Check everything works as expected by running the dev server:
bun run dev
bun run dev
Now we need to change the next.config.ts to:
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
/* config options here */
// cacheComponents: true,
// partialPrefetching: true,
output: 'export',
distDir: 'dist',
trailingSlash: true
}
export default nextConfig
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
/* config options here */
// cacheComponents: true,
// partialPrefetching: true,
output: 'export',
distDir: 'dist',
trailingSlash: true
}
export default nextConfig
We enabled the static export by setting output: 'export'. Also, the output folder was changed to dist (instead of out by default). We also need trailingSlash: true to make the Next.js routes work without the server (change links from /me -> /me/ and emit /me.html -> /me/index.html). Please see the docs for more details.
This script will check if the dist folder exists. Ask whether we need to rebuild the project. Back up the old deployment to the BACKET_NAME.backup bucket. And upload the current build to the BUCKET_NAME bucket.
Please create the BUCKET_NAME.backup bucket. For example, if your bucket name is static-app, then create the static-app.backup bucket.
Also, install the aws-cli command-line tool. We will need it to manipulate our buckets. See the docs for more info.