Skip to content

Commit

Permalink
Added routing for extensionless static files (#14)
Browse files Browse the repository at this point in the history
- updated READMEs
- added amplify.yml back to monorepo
  • Loading branch information
alexnguyennz authored Dec 20, 2023
1 parent 7f73b12 commit acd29cc
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 18 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ frontend:
### Static or prerendered pages
All pages must be server-rendered by default - you can't use `export const prerender = true` on any pages. As a workaround for static pages however, you can set a manual Amplify rewrite for every static route.
To use static pre-rendered pages (e.g. with `export const prerender = true` for `server`, or by default for `hybrid`), you will need to create an Amplify rewrite for every static route.

For example, if you have a static `/about` page, create a rewrite of:

Expand All @@ -130,16 +130,24 @@ For base path routes, create a rewrite of:

`/base/about/ /base/about/index.html 200 (Rewrite)`

### Static files without extensions
Due to limitations with Amplify routing, if you want to serve static files without extensions (from the `public` folder), place them in a folder called `assets` (`/public/assets/`) and reference them in any pages or components with `/assets/filename`.

Any other static files with extensions can be placed anywhere in `public`.

### 404 Pages
Due to limitations with Amplify routing, custom 404 pages (e.g. `404.astro`) need to be SSRed (not prerendered) to work.

## Features

### Supported
- server and hybrid mode
- image optimization with `<Image>` and `<Picture />` (tentative)
- image optimization with `<Image>` and `<Picture />`
- base paths
- middleware

### Unsupported / Untested
- pre-rendered pages (without workaround above)
- Amplify Image optimization
- ???

## Monorepo Project Setup
Expand Down Expand Up @@ -170,7 +178,8 @@ AWS Amplify build specification:
```yaml
version: 1
applications:
- appRoot: demos/blog # change accordingly
# if you want to deploy another demo or change the configuration, you will need to change `amplify.yml` - changing the build spec won't override this file
- appRoot: demos/blog
frontend:
phases:
preBuild:
Expand Down
20 changes: 20 additions & 0 deletions amplify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: 1
applications:
- appRoot: demos/blog
frontend:
phases:
preBuild:
commands:
- npm i -g pnpm
- pnpm config set store-dir .pnpm-store
- pnpm i
build:
commands:
- pnpm run build
artifacts:
baseDirectory: .amplify-hosting
files:
- '**/*'
cache:
paths:
- .pnpm-store/**/*
7 changes: 7 additions & 0 deletions demos/base-path/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# base-path

## 0.0.6

### Patch Changes

- Updated dependencies []:
- [email protected]

## 0.0.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion demos/base-path/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "base-path",
"type": "module",
"private": true,
"version": "0.0.5",
"version": "0.0.6",
"scripts": {
"dev": "astro dev",
"start": "astro dev",
Expand Down
7 changes: 7 additions & 0 deletions demos/blog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# blog

## 0.0.6

### Patch Changes

- Updated dependencies []:
- [email protected]

## 0.0.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion demos/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "blog",
"type": "module",
"private": true,
"version": "0.0.5",
"version": "0.0.6",
"scripts": {
"dev": "astro dev",
"start": "astro dev",
Expand Down
Binary file added demos/blog/public/assets/image
Binary file not shown.
19 changes: 19 additions & 0 deletions demos/blog/src/pages/404.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
import BaseHead from "../components/BaseHead.astro";
import Header from "../components/Header.astro";
import Footer from "../components/Footer.astro";
import { SITE_TITLE, SITE_DESCRIPTION } from "../consts";
---

<!doctype html>
<html lang="en">
<head>
<BaseHead title={SITE_TITLE} description={SITE_DESCRIPTION} />
</head>
<body>
<Header title={SITE_TITLE} />
<main><h1>404 - Page Not Found</h1></main>

<Footer />
</body>
</html>
4 changes: 2 additions & 2 deletions demos/blog/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const response = await fetch(
const data = await response.json();
const locals = Astro.locals;
console.log("data", locals);
---

<!doctype html>
Expand All @@ -37,6 +35,8 @@ console.log("data", locals);
alt=""
/>

<img src="/assets/image" alt="static asset with no extension" />

<p>
Welcome to the official <a href="https://astro.build/">Astro</a> blog
starter template. This template serves as a lightweight,
Expand Down
7 changes: 7 additions & 0 deletions demos/hybrid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# hybrid

## 0.0.6

### Patch Changes

- Updated dependencies []:
- [email protected]

## 0.0.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion demos/hybrid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hybrid",
"type": "module",
"private": true,
"version": "0.0.5",
"version": "0.0.6",
"scripts": {
"dev": "astro dev",
"start": "astro dev",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# astro-aws-amplify

## 0.0.9

### Patch Changes

- Added routing for extensionless static files

## 0.0.8

### Patch Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ frontend:
### Static or prerendered pages
All pages must be server-rendered by default - you can't use `export const prerender = true` on any pages. As a workaround for static pages however, you can set a manual Amplify rewrite for every static route.
To use static pre-rendered pages (e.g. with `export const prerender = true` for `server`, or by default for `hybrid`), you will need to create an Amplify rewrite for every static route.

For example, if you have a static `/about` page, create a rewrite of:

Expand All @@ -130,16 +130,24 @@ For base path routes, create a rewrite of:

`/base/about/ /base/about/index.html 200 (Rewrite)`

### Static files without extensions
Due to limitations with Amplify routing, if you want to serve static files without extensions (from the `public` folder), place them in a folder called `assets` (`/public/assets/`) and reference them in any pages or components with `/assets/filename`.

Any other static files with extensions can be placed anywhere in `public`.

### 404 Pages
Due to limitations with Amplify routing, custom 404 pages (e.g. `404.astro`) need to be SSRed (not prerendered) to work.

## Features

### Supported
- server and hybrid mode
- image optimization with `<Image>` and `<Picture />` (tentative)
- image optimization with `<Image>` and `<Picture />`
- base paths
- middleware

### Unsupported / Untested
- pre-rendered pages (without workaround above)
- Amplify Image optimization
- ???

## Monorepo Project Setup
Expand Down Expand Up @@ -170,7 +178,8 @@ AWS Amplify build specification:
```yaml
version: 1
applications:
- appRoot: demos/blog # change accordingly
# if you want to deploy another demo or change the configuration, you will need to change `amplify.yml` - changing the build spec won't override this file
- appRoot: demos/blog
frontend:
phases:
preBuild:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "astro-aws-amplify",
"version": "0.0.8",
"version": "0.0.9",
"private": false,
"description": "Deploy Astro to AWS Amplify (SSR)",
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export default function amplify(): AstroIntegration {
const deployManifestConfig = {
version: 1,
routes: [
{
path: `${_config.base}assets/*`,
target: {
kind: "Static",
},
},
{
path: `${_config.base}*.*`,
target: {
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit acd29cc

Please sign in to comment.