-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Almost have static rendering working
- Loading branch information
Showing
16 changed files
with
1,192 additions
and
932 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
out | ||
coverage | ||
node_modules | ||
**/out | ||
**/coverage | ||
**/node_modules |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
module.exports = { | ||
collectCoverage: true, | ||
moduleNameMapper: { | ||
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/jest/filemock.js", | ||
"\\.(css|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": | ||
"<rootDir>/jest/filemock.js", | ||
}, | ||
rootDir: "..", | ||
transformIgnorePatterns: ["node_modules/(?!(navi-scripts)/)"], | ||
} |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import React from "react" | ||
import ReactDOM from "react-dom" | ||
import App from "./App" | ||
import Router, { routes } from "./Router" | ||
|
||
it("renders without crashing", (): void => { | ||
window.scroll = () => { | ||
return | ||
} | ||
const div = document.createElement("div") | ||
ReactDOM.render(<App />, div) | ||
ReactDOM.render(<Router routes={routes} />, div) | ||
ReactDOM.unmountComponentAtNode(div) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { default as Navi, lazy, mount, route } from "navi" | ||
import React from "react" | ||
import * as ReactNavi from "react-navi" | ||
import App from "./App" | ||
import PostHeader from "./components/PostHeader" | ||
import Home from "./Home" | ||
import posts from "./posts" | ||
|
||
export default function Router(props: { navigation?: Navi.Navigation; routes?: Navi.Matcher<{}> }) { | ||
return ( | ||
<ReactNavi.Router {...props}> | ||
<App /> | ||
</ReactNavi.Router> | ||
) | ||
} | ||
|
||
function genRoutes() { | ||
const routes: { [path: string]: any } = { | ||
"/": route({ | ||
view: <Home />, | ||
}), | ||
} | ||
|
||
posts.forEach((p, i) => { | ||
routes[p.meta.path] = lazy(async () => { | ||
const Body = (await posts[i].body()).default | ||
return route({ | ||
view: ( | ||
<> | ||
<PostHeader {...p.meta} /> | ||
<Body /> | ||
</> | ||
), | ||
}) | ||
}) | ||
}) | ||
|
||
return routes | ||
} | ||
|
||
export const routes = mount(genRoutes()) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as fs from "fs" | ||
import * as Navi from "navi" | ||
import * as React from "react" | ||
import ReactDOMServer from "react-dom/server" | ||
import Router, { routes } from "../Router" | ||
|
||
async function renderPageToString(path: string) { | ||
const navigation = Navi.createMemoryNavigation({ | ||
routes: routes, | ||
url: Navi.createURLDescriptor(path), | ||
}) | ||
|
||
const route = await navigation.getRoute() | ||
|
||
const rootHTML = ReactDOMServer.renderToString(<Router navigation={navigation} />) | ||
|
||
let indexHTML = fs.readFileSync("out/index.html").toString() | ||
indexHTML = indexHTML.replace(`<div id="root"></div>`, `<div id="root">${rootHTML}</div>`) | ||
if (route.title) { | ||
indexHTML = indexHTML.replace(`<title>nhooyr.io</title>`, `<title>${route.title}</title>`) | ||
} | ||
return indexHTML | ||
} | ||
|
||
async function main() { | ||
const crawledRoutes = await Navi.crawl({ | ||
routes: routes, | ||
}) | ||
for (const p of crawledRoutes.paths) { | ||
const staticHTML = await renderPageToString(p) | ||
let fileName = p.slice(1) | ||
if (fileName === "") { | ||
fileName = "index" | ||
} | ||
fs.writeFileSync(`out/${fileName}.html`, staticHTML) | ||
} | ||
} | ||
|
||
main() |
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,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>nhooyr.io</title> | ||
<meta name="viewport" content="width=device-width" /> | ||
<link rel="shortcut icon" href="favicon.png" /> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -1,13 +1,22 @@ | ||
import * as Navi from "navi" | ||
import React from "react" | ||
import ReactDOM from "react-dom" | ||
import App from "./App" | ||
import "sanitize.css" | ||
import "sanitize.css/forms.css" | ||
import "sanitize.css/typography.css" | ||
import Router, { routes } from "./Router" | ||
|
||
// https://alxgbsn.co.uk/2011/10/17/enable-css-active-pseudo-styles-in-mobile-safari/ | ||
document.body.ontouchstart = () => null | ||
async function main() { | ||
// https://alxgbsn.co.uk/2011/10/17/enable-css-active-pseudo-styles-in-mobile-safari/ | ||
document.body.ontouchstart = () => null | ||
|
||
const root = document.createElement("div") | ||
document.body.appendChild(root) | ||
ReactDOM.render(<App />, root) | ||
const navigation = Navi.createBrowserNavigation({ routes }) | ||
// Ensures the current route is fully loaded to avoid flashes. | ||
await navigation.getRoute() | ||
|
||
const renderer = process.env.NODE_ENV === "production" ? ReactDOM.hydrate : ReactDOM.render | ||
|
||
renderer(<Router navigation={navigation} />, document.getElementById("root")) | ||
} | ||
|
||
main() |
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
File renamed without changes.
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
Oops, something went wrong.