Skip to content

Commit 3a1e5de

Browse files
committed
Finalize CLI params parsing
1 parent 4adecbc commit 3a1e5de

14 files changed

+1006
-2018
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
/dist/*
33
!/dist/prerender-github-action
4-
/sandbox
4+
/sandbox
5+
/.actrc

README.md

+105-17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,86 @@
1-
TODO:
2-
3-
- CLI & args
1+
## TODO:
2+
3+
- tag v1
4+
- CLI: --no-http-server
5+
- Remove sitemap output dir???? + rename sitemap option to be a single one for
6+
enabling and cannonicalUrl
7+
- Publish npm package 0.9
8+
9+
# `prerender-spa-ultra`
10+
11+
Converts your `JavaScript`-powered
12+
[SPA](https://developer.mozilla.org/en-US/docs/Glossary/SPA 'Single Page Application')
13+
web application into individual `*.html` documents (_one for each unique `URL`
14+
your app has_) filled with the content that your `JavaScript` generated for
15+
them.
16+
17+
### When is this useful?
18+
19+
1. When you use `/deep/link/to/a/page` and you want to get a nice preview of
20+
that page when its `URL` is shared.
21+
22+
Link sharing over most channels like messaging apps, social networks or other
23+
websites will not run `JavaScript`, so they will preview your `URL` based on
24+
your static `*.html` (_which is likely empty_). So if you want to preview
25+
meaningful things that are shown with JavaScript like the the page main
26+
image, or the correct page title you want to `prerender` and convert that
27+
empty static html to multiple files (_one per unique URL_) filled with the
28+
corresponding content.
29+
30+
2. When you expect slow connection to your website or huge size of your
31+
JavaScript. Instead of making your users wait staring at a blank page you can
32+
`prerender` that page, so that the html that gets loaded includes the
33+
corresponding content and only make the users wait for `JavaScript`
34+
35+
## Usage
36+
37+
1. As a `github action` ([_see all settings_](#github-action)):
38+
```yaml
39+
uses: antitoxic/prerender-spa-ultra@v1
40+
with:
41+
website_root: 'path/to/your/spa/dist'
42+
```
43+
2. As `CLI` ([_see options_](#cli)):
44+
1. serve your
45+
[SPA](https://developer.mozilla.org/en-US/docs/Glossary/SPA 'Single Page Application')
46+
static files:
47+
```shell
48+
cd <path/to/your/output/dir>
49+
```
50+
And then you can pick one of 2 options:
51+
52+
1. using a python 3 file included in `prerender-spa-ultra`
53+
```shell
54+
python <path/to/node_modules>/prerender-spa-ultra/src/http-server.py
55+
```
56+
2. using the `node-static` package
57+
```shell
58+
npx node-static --spa
59+
npx http-server --proxy "http://localhost:8080?"
60+
npx serve --single
61+
```
62+
2. run prerender:
63+
```shell
64+
npx prerender-spa-ultra <path/to/your/output/dir>
65+
```
66+
3. As an `npm` package ([_see all settings_](#github-action)):
67+
68+
```typescript
69+
import { preRenderSite } from 'prerender-spa-ultra';
70+
await preRenderSite({
71+
outputDir: 'path/to/output',
72+
startingUrl: 'http://localhost:8000',
73+
});
74+
```
475

5-
1. Start local http server in your output dir using:
76+
4. Start local http server in your web root (_also output_) dir using:
677
```shell
778
cd <path/to/your/output/dir>
879
python <path/to/node_modules>/prerender-spa-ultra/src/http-server.py
980
```
10-
2. use this domain when pre-rendering
81+
5. use this domain when pre-rendering
1182

12-
# Goals
83+
## Goals
1384

1485
- **Simplest prerender out there**
1586
- Single dependency (`puppeteer-core`), nothing else. This also means saving
@@ -44,7 +115,8 @@ TODO:
44115

45116
```shell
46117
PRERENDER_SPA_ULTRA_DEBUG=1 npx prerender-spa-ultra ....
47-
PRERENDER_SPA_ULTRA_DEBUG=1 <your command that uses this package>
118+
# or
119+
PRERENDER_SPA_ULTRA_DEBUG=1 <your command that uses this package programatically>
48120
```
49121

50122
## Non-goals
@@ -66,7 +138,7 @@ PRERENDER_SPA_ULTRA_DEBUG=1 <your command that uses this package>
66138
installed. Otherwise you can use the OS package manager to install or use
67139
`node-chromium` (`npm install chromium`)
68140

69-
# Prior art
141+
## Prior art
70142

71143
- https://github.com/egoist/presite
72144
- https://github.com/stereobooster/react-snap
@@ -75,32 +147,36 @@ PRERENDER_SPA_ULTRA_DEBUG=1 <your command that uses this package>
75147
- https://github.com/sPavl0v/react-spa-prenderer
76148
- https://github.com/chrisvfritz/prerender-spa-plugin
77149

78-
# External Pre-render Services available
150+
## External Pre-render Services available
79151

80152
- https://webprerender.io/pricing/
81153
- https://prerender.io/
82154

83-
# Marketing locations
155+
## Marketing locations
84156

85157
- https://github.com/automata/awesome-jamstack
86158
- (old) https://www.tnd.dev/tool/
87159

88160
# Limitations & Caveats
89161

90-
1. This library shares a limitation that any pre-rendering intended for use by a
91-
static file server: it doesn't work with hash-based routing of SPAs (i.e.
92-
`example.com/#route`). This is because the server never sees the `#...` part
93-
of the url so it can't find a file based it. If you are in this scenario you
94-
can try migrating to html5 push history
162+
1. This library shares 2 limitations that any other pre-rendering lib has:
163+
1. It doesn't work with hash-based routing of SPAs (i.e.
164+
`example.com/#route`). This is because the server never sees the `#...`
165+
part of the url so it can't find a file based it. If you are in this
166+
scenario you can try migrating to html5 push history.
167+
2. Your assets (_media files, `*.js`, `*.css`_) should not be linked as
168+
relative paths since pre-rendering creates a nested folder structure to
169+
match the urls you have. Instead, those should be linked from your URL
170+
root (`/...`).
95171
2. The library will ignore query params and consider all urls having matching
96172
`pathname` to be one and the same url. You can override this by providing a
97-
custom `cleanUrl` and `getFilename` functions to `prerenderSite`
173+
custom `cleanUrl` and `getFilename` functions to `preRenderSite`
98174
3. The default `cleanUrl` trims any slashes, which means `some/url/path/` and
99175
`some/url/path` will be considered the same
100176

101177
# Background & References
102178

103-
- Cannot use JSDOM since it can't safely execute `<script/>`s
179+
- Not ideal to use JSDOM since it can't safely execute `<script/>`s
104180
- Good jam-stack intro video: https://vimeo.com/163522126 and the rest:
105181
https://jamstack.org/resources/videos/
106182
- Available binary packages in various CI/CD pre-build images:
@@ -112,3 +188,15 @@ PRERENDER_SPA_ULTRA_DEBUG=1 <your command that uses this package>
112188
- Github actions running locally:
113189
- https://stackoverflow.com/questions/59241249/how-to-run-github-actions-workflows-locally
114190
- https://github.com/nektos/act
191+
192+
## Cloudflare integration
193+
194+
https://developers.cloudflare.com/workers/wrangler/ci-cd/
195+
196+
To use cloudflare cli (`wrangler`) from CI like github actions you need to
197+
create `CLOUDFLARE_API_TOKEN` and add it as a secret in that CI environment
198+
199+
Maybe you will need to set `CLOUDFLARE_ACCOUNT_ID` if you have more than 1
200+
accounts associated with this API token.
201+
202+
Must expose github secret as ENV variable (not done by default)

0 commit comments

Comments
 (0)