Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat(gatsby-plugin-fastity): Gatsby v4 DSG & SSR Support #68

Merged
merged 47 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
29876a4
wip, kinda working a little quirky
moonmeister Sep 20, 2021
46c989d
Formatting with Prettier
actions-user Sep 20, 2021
fa55e67
revert: config changes
moonmeister Sep 21, 2021
db13bf0
initial wip
moonmeister Sep 21, 2021
a6e7005
feat: gatsby v4 implementation. Still waiting on fastify-static bug fix
moonmeister Sep 21, 2021
5a37d33
Formatting with Prettier
actions-user Sep 21, 2021
0be9d8b
feat: move config file to cache. Enable refresh endpoint (not sure if…
moonmeister Sep 22, 2021
2fafede
Merge branch 'feat/fastify/gatsby-v4' of github.com:gatsby-uc/plugins…
moonmeister Sep 22, 2021
13efc0f
docs: readme updates
moonmeister Sep 23, 2021
abd07d7
Merge branch 'main' into feat/fastify/gatsby-v4
moonmeister Sep 24, 2021
672998f
fix: formatting and extra package
moonmeister Sep 24, 2021
58edd4f
Merge branch 'main' into feat/fastify/gatsby-v4
moonmeister Oct 8, 2021
93a7eca
Merge branch 'main' into feat/fastify/gatsby-v4
moonmeister Oct 15, 2021
4396080
feat: handle 500 for ssr/dsg render
moonmeister Oct 15, 2021
37ec975
fix: unique 404 for API routes
moonmeister Oct 16, 2021
7295503
Merge branch 'main' into feat/fastify/gatsby-v4
moonmeister Oct 21, 2021
313ca35
Post merge fixes and v4 GA updates
moonmeister Oct 21, 2021
a05f10b
fix: handle folks not providing Gatsby 404
moonmeister Oct 21, 2021
b076105
Fix ico causeing fastify error
moonmeister Oct 21, 2021
f15e3e0
Merge branch 'main' into feat/fastify/gatsby-v4
moonmeister Nov 1, 2021
1bc8920
cleanup yarn
moonmeister Nov 1, 2021
8175603
chore: remove unneeded refresh endpoint code
moonmeister Nov 1, 2021
41bcf3a
chore: remove refresh link
moonmeister Nov 1, 2021
7d81c2a
feat: reimplement SSR/DSG to register static routes since new routes …
moonmeister Nov 1, 2021
0d444c0
chore: cleanup and improve logging
moonmeister Nov 1, 2021
116edab
Merge branch 'main' into feat/fastify/gatsby-v4
moonmeister Nov 1, 2021
7c7519e
cleaner logging
moonmeister Nov 1, 2021
25dd6ed
start benchmarking wip
moonmeister Nov 1, 2021
1a19242
merge/update
moonmeister Nov 1, 2021
1050f53
feat: add benchmarks for ssr/dsg, fix: overly specific html requireme…
moonmeister Nov 2, 2021
b34f82e
fix: correct settings
moonmeister Nov 2, 2021
0b45332
fix undefined error when calling sendFile, update jest, change node v…
moonmeister Nov 2, 2021
cf7475e
Merge branch 'feat/fastify/gatsby-v4' of github.com:gatsby-uc/plugins…
moonmeister Nov 2, 2021
d4463dc
chore: misc cleanup
moonmeister Nov 2, 2021
4a513ff
misc: various cleanup, docs, etc
moonmeister Nov 2, 2021
7da58c6
chore: docs, cleanup
moonmeister Nov 2, 2021
a125ac0
chore: move test site
moonmeister Nov 4, 2021
362c15b
chore: add debug config
moonmeister Nov 4, 2021
a992831
fix: testing was broke
moonmeister Nov 4, 2021
e4e51f6
chore: remove undescessary check for cache folder
moonmeister Nov 4, 2021
89b088f
fix: make sure error is dumped to console
moonmeister Nov 4, 2021
8ffbacd
chore: cleanup benchmark
moonmeister Nov 5, 2021
e9111a6
chore: cleanup and some more logs
moonmeister Nov 5, 2021
4346a98
fix: pretest
moonmeister Nov 5, 2021
842e4db
test: tests for dsg/ssr support
moonmeister Nov 5, 2021
cbeb8b3
remove incorrect cs
moonmeister Nov 5, 2021
74c6f35
fix: remove console log in tests
moonmeister Nov 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/good-plums-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gatsby-plugin-fastify": minor
---

feat: much improved logging
5 changes: 5 additions & 0 deletions .changeset/kind-starfishes-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gatsby-plugin-fastify": patch
---

Fix: SSR/DSG routes throwing non-fatal error when returning file.
5 changes: 5 additions & 0 deletions .changeset/odd-days-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gatsby-plugin-fastify": minor
---

feat: add support for Gatsby 500 error if SSR/DSG throws an error.
5 changes: 5 additions & 0 deletions .changeset/stale-rocks-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gatsby-plugin-fastify": patch
---

fix: handle situations where dev doesn't provide 404 page by falling back to generic 404.
5 changes: 5 additions & 0 deletions .changeset/strange-adults-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gatsby-plugin-fastify": minor
---

Support for Gatsby v4's DSG and SSR routes.
546 changes: 546 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

768 changes: 768 additions & 0 deletions .yarn/releases/yarn-3.1.0-rc.12.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ nodeLinker: node-modules
plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"

yarnPath: .yarn/releases/yarn-3.1.0.cjs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"private": true,
"workspaces": [
"packages/*",
"packages/**/test-site"
"test-sites/*"
],
"scripts": {
"ac": "yarn all-contributors",
Expand Down
22 changes: 22 additions & 0 deletions packages/gatsby-plugin-fastify/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach",
"port": 9229,
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node"
},
{
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node"
}
]
}
9 changes: 7 additions & 2 deletions packages/gatsby-plugin-fastify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

- Serving Gatsby Functions
- Serving static files
- Serving DSG/SSR Routes
- Gatsby 404 page
- Gatsby 500 page
- Gatsby redirects
- Client-side paths
- Serving the site with pathPrefix - set it up inside `gatsby-config.js`, the plugin will take care of it
Expand All @@ -33,7 +35,7 @@
Install the plugin using npm or yarn

```sh
npm install gatsby-plugin-fastify fastify fastify-static fastify-compress fastify-plugin
npm install gatsby-plugin-fastify fastify fastify-static fastify-compress fastify-plugin fastify-accepts
```

and add it to your `gatsby-config.js`
Expand All @@ -56,6 +58,8 @@ module.exports = {

# Serving your site

Node and Fastify are great for building application specific web servers but generally should not be used on the edge. Meaning, most folks will use a fully fledged web server (e.g. [Nginx](https://www.nginx.com/) or [Caddy](https://caddyserver.com/) that handles traffic before passing it back to node. This allows the Edge web server to handle security, TLS/SSL, load balencing, etc. Then the node server only worries about the application. A CDN (e.g. Fastly or CloudFlare ) is also often used for performance and scalability.

## Server CLI (expected)

This plugin implements a server that's ready to go. To use this you can configure a `start`(or whatever you prefer) command in your `package.json`:
Expand Down Expand Up @@ -111,12 +115,13 @@ For an example on how to use thi reference the server implementation file from [
Finally, each of the Gatsby features (functions, static files, redirects, client-only routes, and 404 handling) is implemented in it's own plugin. Those may be imported as well for use in a custom server implementation.

```js
import { handle } from "gatsby-plugin-fastify/plugins/gatsby";
import { handle404 } from "gatsby-plugin-fastify/plugins/404";
import { handle500 } from "gatsby-plugin-fastify/plugins/500";
import { handleClientOnlyPaths } from "gatsby-plugin-fastify/plugins/clientPaths";
import { handleFunctions } from "gatsby-plugin-fastify/plugins/functions";
import { handleRedirects } from "gatsby-plugin-fastify/plugins/redirects";
import { handleStatic } from "gatsby-plugin-fastify/plugins/static";
import { handleServerRoutes } from "gatsby-plugin-fastify/plugins/serverRoutes";
```

For an example on how to use these you see the `serveGatsby` implementation file from [`src/plugins/gatsby.ts`](https://github.com/gatsby-uc/plugins/tree/main/packages/gatsby-plugin-fastify/src/plugins/gatsby.ts).
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-plugin-fastify/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module.exports = {
],

modulePathIgnorePatterns: [`<rootDir>/test-site/`],
transformIgnorePatterns: ["/query-engine/", "/page-ssr/", "/test-sites/fastify/"],
coveragePathIgnorePatterns: ["/query-engine/", "/page-ssr/", "/test-sites/fastify/"],
// The test environment that will be used for testing
testEnvironment: `node`,
};
14 changes: 8 additions & 6 deletions packages/gatsby-plugin-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"scripts": {
"build": "babel src --out-dir . --ignore \"**/__tests__,**/*.d.ts\" --extensions \".ts,.js\"",
"pretest": "cd test-site && yarn run build",
"pretest": "cd ../../test-sites/fastify/ && yarn run build",
"test": "jest --coverage",
"test:watch": "jest --watch",
"watch": "yarn build --watch"
Expand All @@ -48,21 +48,23 @@
"babel-preset-gatsby-package": "^1.14.0",
"cross-env": "^7.0.3",
"fastify": "^3.22.0",
"fastify-accepts": "^2.0.1",
"fastify-compress": "^3.6.0",
"fastify-plugin": "^3.0.0",
"fastify-static": "^4.4.0",
"gatsby": "^3.14.2",
"gatsby-plugin-utils": "^1.14.0",
"jest": "^27.2.5"
"gatsby": "^4.0.0",
"gatsby-plugin-utils": "^2.0.0",
"jest": "^27.3.1"
},
"peerDependencies": {
"fastify": "^3.19.0",
"fastify-accepts": "^2.1.0",
"fastify-compress": "^3.6.0",
"fastify-plugin": "^3.0.0",
"fastify-static": "^4.2.0",
"gatsby": "^3.7.0"
"gatsby": "^4.0.0"
},
"engines": {
"node": ">=12.13.0"
"node": ">=14.15.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,15 @@ Object {
"toPath": "/posts/page-3",
},
],
"serverSideRoutes": Array [
Object {
"mode": "SSR",
"path": "/ssr",
},
Object {
"mode": "DSG",
"path": "/my/dsg/path",
},
],
}
`;
22 changes: 22 additions & 0 deletions packages/gatsby-plugin-fastify/src/__tests__/__utils__/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Fastify from "fastify";
import { getConfig } from "../../utils/config";

exports.createCliConfig = function createCliConfig({ host, port, logLevel, open }) {
return {
host,
Expand All @@ -10,3 +13,22 @@ exports.createCliConfig = function createCliConfig({ host, port, logLevel, open
o: open,
};
};

exports.createFastifyInstance = async function createFastifyInstance(plugin) {
const {
cli: { logLevel },
} = getConfig();
const fastify = Fastify({
ignoreTrailingSlash: true,
logger: { level: logLevel, prettyPrint: true },
disableRequestLogging: ["trace", "debug"].includes(logLevel) ? false : true,
});

const {
server: { prefix },
} = getConfig();
await fastify.register(plugin, { prefix });
await fastify.ready();

return fastify;
};
10 changes: 9 additions & 1 deletion packages/gatsby-plugin-fastify/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { onPostBuild } = require("../gatsby-node");
const fs = require("fs-extra");

jest.mock("../../utils/constants", () => ({
jest.mock("../utils/constants", () => ({
...jest.requireActual("../utils/constants"),
PATH_TO_FUNCTIONS: "test-site/.cache/functions/",
PATH_TO_PUBLIC: __dirname + "/__files__/public/",
Expand Down Expand Up @@ -63,6 +63,14 @@ const store = {
matchPath: "/app/*",
path: "/app/[...]/",
},
{
path: "/ssr",
mode: "SSR",
},
{
path: "/my/dsg/path",
mode: "DSG",
},
],
redirects: [
{
Expand Down
Loading