-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit 'cac15567ec814b501e502319e02b9d3a3f2d550e' into release
- Loading branch information
Showing
40 changed files
with
1,064 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
# vercel | ||
.vercel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2021 Felix Haus (milliVolt infrastructure) | ||
Copyright (c) 2021 Vercel, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Next.js export and S3 example | ||
|
||
This example shows how to use the image optimizer together with a statically exported Next.js HTML site that is hosted on AWS S3. | ||
|
||
> **Note:** The full example code is available on [GitHub](https://github.com/milliHQ/terraform-aws-next-js-image-optimization/tree/main/examples/with-next-js-export) | ||
## Features | ||
|
||
- ✅ [Statically exported Next.js](https://nextjs.org/docs/advanced-features/static-html-export) HTML site using `next export` | ||
- ✅ Site hosted on [AWS S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteHosting.html) | ||
- ✅ Optimized caching with Amazon CloudFront | ||
- ✅ [Serverless image optimization](https://github.com/milliHQ/terraform-aws-next-js-image-optimization) using CloudFront and AWS Lambda | ||
|
||
## Setup guide | ||
|
||
This guide walks you through the necessary steps to deploy the example to your own AWS account. | ||
|
||
### 0. Prerequisites | ||
|
||
The following software is required to follow this guide: | ||
|
||
- [Node.js 14+](https://nodejs.org/) | ||
- [Terraform 1.0+](https://www.terraform.io/downloads) | ||
- [AWS CLI](https://aws.amazon.com/cli/) | ||
|
||
### 1. Initialize project | ||
|
||
Download the files from the example app and initialize it in a new folder: | ||
|
||
```sh | ||
npx create-next-app -e https://github.com/milliHQ/terraform-aws-next-js-image-optimization/tree/main/examples/with-next-js-export my-app | ||
cd my-app | ||
``` | ||
|
||
### 2. Create the required resources in your AWS account | ||
|
||
We use Terraform to create all the resources (S3 bucket, CloudFront distribution, image optimizer, etc.) in your AWS account. | ||
For simplicity we use [AWS Access Keys](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-appendix-sign-up.html) for authentication, other authentication methods are [also available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#authentication). | ||
|
||
```sh | ||
# Expose your AWS Access Keys to the current terminal session | ||
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE | ||
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY | ||
|
||
terraform init # Only needed on the first time running Terraform | ||
|
||
terraform plan # (Optional) See what resources Terraform will create | ||
terraform apply # Deploy the image optimizer module to your AWS account | ||
``` | ||
|
||
After Terraform has successfully created all resources in your AWS account, you should see the following output on the terminal: | ||
|
||
```sh | ||
> Apply complete! | ||
> | ||
> Outputs: | ||
> | ||
> domain = "<distribution-id>.cloudfront.net" | ||
> website_bucket_id = "next-image-optimizer-example-export" | ||
``` | ||
|
||
### 3. Adjust Next.js config | ||
|
||
In the Next.js project, open the `next.config.js` file and add the following lines (Remember to replace `<distribution-id>` with the output from the previous step): | ||
|
||
```diff | ||
// next.config.js | ||
+ const domainName = 'xxxxxxxxxxxxxxxxx.cloudfront.net'; | ||
- const domainName = '<distribution-id>.cloudfront.net'; | ||
``` | ||
|
||
### 4. Build and export the Next.js site | ||
|
||
To create a static build of Next.js we need to run two commands. | ||
`next build` builds the production site in the `.next/` folder. | ||
[`next export`](https://nextjs.org/docs/advanced-features/static-html-export) then creates a static HTML export from your site that can be deployed to a static file hosting service like AWS S3. | ||
|
||
```sh | ||
npm run build # Builds the Next.js site | ||
npm run export # Prepares the | ||
``` | ||
|
||
After running the two commands you should see a new folder named `out/` in your project, where the statically exported site is exported to. | ||
|
||
### 5. Upload the Next.js site to S3 | ||
|
||
To upload the content of the `out/` folder we use the [AWS CLI](https://aws.amazon.com/cli/). | ||
Make sure to replace `<bucket-name>` with the output from step 2 before running the command: | ||
|
||
```sh | ||
aws s3 sync ./out s3://<bucket-name> | ||
``` | ||
|
||
--- | ||
|
||
Your website should now be accessible on the CloudFront domain `<distribution-id>.cloudfront.net` you got from step 2. | ||
To update your site, simply repeat step 4. & 5 of this guide. | ||
|
||
## Caveats | ||
|
||
Please note that [not all features](https://nextjs.org/docs/advanced-features/static-html-export#unsupported-features) of Next.js are supported when using `next export`. | ||
In addition here are some things that should be taken into account when using this solution: | ||
|
||
- **When using JPEG images in the `/public` folder, always use the `.jpeg` (long) not the `.jpg` (short) extension for it.** | ||
Next.js image component always requests the long extension `.jpeg`, since S3 cannot soft redirect `.jpg` to `.jpeg` the request for the image would fail otherwise. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
body { | ||
font-family: -apple-system, BlinkMacSystemFont, sans-serif; | ||
margin: 0; | ||
padding: 0; | ||
background: black; | ||
color: white; | ||
} | ||
|
||
a { | ||
cursor: pointer; | ||
color: #0076ff; | ||
text-decoration: none; | ||
transition: all 0.2s ease; | ||
border-bottom: 1px solid black; | ||
} | ||
|
||
a:hover { | ||
border-bottom: 1px solid #0076ff; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { svg, arm } from './view-source.module.css'; | ||
|
||
const ViewSource = ({ pathname }) => ( | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="80" | ||
height="80" | ||
viewBox="0 0 250 250" | ||
fill="#151513" | ||
className={svg} | ||
> | ||
<a | ||
title="View Source" | ||
href={`https://github.com/vercel/next.js/tree/canary/examples/image-component/${pathname}`} | ||
> | ||
<path d="M0 0l115 115h15l12 27 108 108V0z" fill="#fff" /> | ||
<path | ||
className={arm} | ||
d="M128 109c-15-9-9-19-9-19 3-7 2-11 2-11-1-7 3-2 3-2 4 5 2 11 2 11-3 10 5 15 9 16" | ||
/> | ||
<path d="M115 115s4 2 5 0l14-14c3-2 6-3 8-3-8-11-15-24 2-41 5-5 10-7 16-7 1-2 3-7 12-11 0 0 5 3 7 16 4 2 8 5 12 9s7 8 9 12c14 3 17 7 17 7-4 8-9 11-11 11 0 6-2 11-7 16-16 16-30 10-41 2 0 3-1 7-5 11l-12 11c-1 1 1 5 1 5z" /> | ||
</a> | ||
</svg> | ||
); | ||
|
||
export default ViewSource; |
48 changes: 48 additions & 0 deletions
48
examples/with-next-js-export/components/view-source.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
.svg { | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
} | ||
.arm { | ||
transform-origin: 130px 106px; | ||
} | ||
|
||
.svg:hover .arm { | ||
animation: wave 560ms ease-in-out; | ||
} | ||
|
||
@keyframes wave { | ||
0% { | ||
transform: rotate(0deg); | ||
} | ||
|
||
20% { | ||
transform: rotate(-25deg); | ||
} | ||
|
||
40% { | ||
transform: rotate(10deg); | ||
} | ||
|
||
60% { | ||
transform: rotate(-25deg); | ||
} | ||
|
||
80% { | ||
transform: rotate(10deg); | ||
} | ||
|
||
100% { | ||
transform: rotate(0deg); | ||
} | ||
} | ||
|
||
@media (max-width: 500px) { | ||
.svg:hover .arm { | ||
animation: none; | ||
} | ||
|
||
.svg:hover .arm { | ||
animation: wave 560ms ease-in-out; | ||
} | ||
} |
Oops, something went wrong.