Skip to content

Commit

Permalink
Merge branch 'main' into logan/native-sourcemaps
Browse files Browse the repository at this point in the history
Signed-off-by: Logan McAnsh <[email protected]>
  • Loading branch information
mcansh authored Oct 16, 2023
2 parents 4c6aebf + 0b3e1cd commit 30268ec
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 199 deletions.
2 changes: 1 addition & 1 deletion packages/remix-fastify/LICENSE → LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of

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.
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.
8 changes: 4 additions & 4 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
"@remix-run/react": "^2.0.1",
"@remix-run/serve": "^2.0.1",
"chokidar": "^3.5.3",
"fastify": "^4.23.2",
"fastify": "^4.24.2",
"isbot": "^3.7.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@remix-run/dev": "^2.0.1",
"@remix-run/eslint-config": "^2.0.1",
"@types/react": "^18.2.24",
"@types/react-dom": "^18.2.8",
"eslint": "^8.50.0",
"@types/react": "^18.2.28",
"@types/react-dom": "^18.2.13",
"eslint": "^8.51.0",
"typescript": "^5.2.2"
},
"engines": {
Expand Down
34 changes: 20 additions & 14 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
staticFilePlugin,
getEarlyHintLinks,
} from "@mcansh/remix-fastify";
import { installGlobals, broadcastDevReady } from "@remix-run/node";
// early hints are optional, feel free to remove this along with lines 7, 33, 47-48, and 87-88
import { broadcastDevReady, installGlobals } from "@remix-run/node";
import sourceMapSupport from "source-map-support";
import { fastifyEarlyHints } from "@fastify/early-hints";

installGlobals();
Expand All @@ -17,8 +17,20 @@ let VERSION_PATH = "./build/version.txt";

/** @typedef {import('@remix-run/node').ServerBuild} ServerBuild */

/** @type {ServerBuild} */
let initialBuild = await import(BUILD_PATH);

let handler;

if (process.env.NODE_ENV === "development") {
handler = await createDevRequestHandler(initialBuild);
} else {
handler = createRequestHandler({
build: initialBuild,
mode: initialBuild.mode,
});
}

let app = fastify();

let noopContentParser = (_request, payload, done) => {
Expand All @@ -31,24 +43,18 @@ app.addContentTypeParser("*", noopContentParser);
await app.register(fastifyEarlyHints, { warn: true });

// match with remix.config
app.register(staticFilePlugin, {
await app.register(staticFilePlugin, {
assetsBuildDirectory: "public/build",
publicPath: "/build/",
});

app.all("*", async (request, reply) => {
if (process.env.NODE_ENV === "development") {
let devHandler = await createDevRequestHandler(initialBuild);
return devHandler(request, reply);
if (process.env.NODE_ENV === "production") {
let links = getEarlyHintLinks(request, initialBuild);
await reply.writeEarlyHintsLinks(links);
}

let links = getEarlyHintLinks(request, initialBuild);
await reply.writeEarlyHintsLinks(links);

return createRequestHandler({
build: initialBuild,
mode: initialBuild.mode,
})(request, reply);
return handler(request, reply);
});

let port = process.env.PORT ? Number(process.env.PORT) || 3000 : 3000;
Expand All @@ -63,7 +69,7 @@ if (process.env.NODE_ENV === "development") {
/**
* @param {ServerBuild} initialBuild
* @param {import('@mcansh/remix-fastify').GetLoadContextFunction} [getLoadContext]
* @returns {import('@remix-run/express').RequestHandler}
* @returns {Promise<import('@mcansh/remix-fastify').RequestHandler>}
*/
async function createDevRequestHandler(initialBuild, getLoadContext) {
let build = initialBuild;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
"@manypkg/get-packages": "^2.2.0",
"@npmcli/package-json": "^5.0.0",
"@remix-run/eslint-config": "2.0.1",
"eslint": "^8.50.0",
"eslint": "^8.51.0",
"eslint-plugin-prefer-let": "^3.0.1",
"glob": "^10.3.10",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
"publint": "^0.2.3",
"publint": "^0.2.4",
"react": "^18.2.0",
"semver": "^7.5.4",
"tsup": "^7.2.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/remix-fastify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Use [Remix](https://remix.run) with [Fastify](http://fastify.io)
## Manual Installation

```sh
npm i @mcansh/remix-fastify fastify
yarn add @mcansh/remix-fastify fastify
pnpm i @mcansh/remix-fastify fastify
npm i @mcansh/remix-fastify fastify @fastify/early-hints source-map-support
yarn add @mcansh/remix-fastify fastify @fastify/early-hints source-map-support
pnpm i @mcansh/remix-fastify fastify @fastify/early-hints source-map-support
```
6 changes: 3 additions & 3 deletions packages/remix-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"LICENSE"
],
"scripts": {
"prepublishOnly": "npm run build && publint",
"prepublishOnly": "npm run build && cp ../../LICENSE LICENSE && publint",
"typecheck": "tsc",
"dev": "tsup --watch",
"build": "tsup",
Expand All @@ -48,9 +48,9 @@
"@remix-run/node": "^2.0.1",
"@remix-run/server-runtime": "^2.0.1",
"@types/glob": "^8.1.0",
"@types/node": "^20.6.5",
"@types/node": "^20.8.6",
"@typescript/lib-dom": "npm:@types/web@^0.0.115",
"fastify": "^4.23.2",
"fastify": "^4.24.2",
"node-mocks-http": "^1.13.0",
"typescript": "^5.2.2"
},
Expand Down
8 changes: 3 additions & 5 deletions playground/app/routes/_layout._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ export default function Index() {
<div>
<React.Suspense fallback={<h2>loading...</h2>}>
<Await resolve={data.name} errorElement={<h2>failed...</h2>}>
{(resolvedName) => (
<h2>
Hello {resolvedName}, with context name {data.loadContextName}
</h2>
)}
{(resolvedName) => <h2>Hello {resolvedName}</h2>}
</Await>
</React.Suspense>

<h2>Context: {data.loadContextName}</h2>

<Form
method="post"
style={{ display: "flex", justifyContent: "center", gap: 4 }}
Expand Down
8 changes: 4 additions & 4 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@
"@mcansh/remix-fastify": "workspace:*",
"@remix-run/node": "^2.0.1",
"@remix-run/react": "^2.0.1",
"fastify": "^4.23.2",
"fastify": "^4.24.2",
"isbot": "^3.7.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@remix-run/dev": "^2.0.1",
"@remix-run/eslint-config": "^2.0.1",
"@types/react": "^18.2.24",
"@types/react-dom": "^18.2.8",
"@types/react": "^18.2.28",
"@types/react-dom": "^18.2.13",
"chokidar": "^3.5.3",
"cross-env": "^7.0.3",
"dotenv": "^16.3.1",
"eslint": "^8.50.0",
"eslint": "^8.51.0",
"npm-run-all": "^4.1.5",
"typescript": "^5.2.2"
},
Expand Down
36 changes: 19 additions & 17 deletions playground/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,22 @@ let VERSION_PATH = "./build/version.txt";

/** @typedef {import('@remix-run/node').ServerBuild} ServerBuild */

/** @type {ServerBuild} */
let initialBuild = await import(BUILD_PATH);

let handler;
let getLoadContext = () => ({ loadContextName: "John Doe" });

if (process.env.NODE_ENV === "development") {
handler = await createDevRequestHandler(initialBuild, getLoadContext);
} else {
handler = createRequestHandler({
build: initialBuild,
mode: initialBuild.mode,
getLoadContext,
});
}

let app = fastify();

let noopContentParser = (_request, payload, done) => {
Expand All @@ -34,24 +48,12 @@ await app.register(staticFilePlugin, {
});

app.all("*", async (request, reply) => {
let getLoadContext = () => ({ loadContextName: "John Doe" });

if (process.env.NODE_ENV === "development") {
let devHandler = await createDevRequestHandler(
initialBuild,
getLoadContext,
);
return devHandler(request, reply);
if (process.env.NODE_ENV === "production") {
let links = getEarlyHintLinks(request, initialBuild);
await reply.writeEarlyHintsLinks(links);
}

let links = getEarlyHintLinks(request, initialBuild);
await reply.writeEarlyHintsLinks(links);

return createRequestHandler({
build: initialBuild,
getLoadContext,
mode: initialBuild.mode,
})(request, reply);
return handler(request, reply);
});

let port = process.env.PORT ? Number(process.env.PORT) || 3000 : 3000;
Expand All @@ -66,7 +68,7 @@ if (process.env.NODE_ENV === "development") {
/**
* @param {ServerBuild} initialBuild
* @param {import('@mcansh/remix-fastify').GetLoadContextFunction} [getLoadContext]
* @returns {import('@remix-run/express').RequestHandler}
* @returns {Promise<import('@mcansh/remix-fastify').RequestHandler>}
*/
async function createDevRequestHandler(initialBuild, getLoadContext) {
let build = initialBuild;
Expand Down
Loading

0 comments on commit 30268ec

Please sign in to comment.