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

feat: add @remix-run/netlify-edge package + update Netlify template #3104

Closed
wants to merge 136 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
136 commits
Select commit Hold shift + click to select a range
03b037f
feat(EXPERIMENTAL): add support for Netlify Edge Functions (#2866)
ascorbic Apr 19, 2022
299f041
chore: patches for pre
chaance Apr 19, 2022
a98927a
chore: add netlify-edge to version script
mcansh Apr 19, 2022
0558147
chore: revert override of --template flag
chaance Apr 19, 2022
3beff72
fix: bundle netlify-edge (#2931)
ascorbic Apr 20, 2022
803c643
fix: updated conditions for deno and netlify edge (#2942)
jacob-ebey Apr 20, 2022
d092036
Revert "chore: patches for pre"
nickytonline May 5, 2022
e7fb4cb
Update packages/remix-netlify-edge/package.json
nickytonline May 6, 2022
74bb317
Update packages/remix-netlify-edge/package.json
nickytonline May 6, 2022
590c309
Removed accidentally added package-lock.json from Netlify Edge template
nickytonline May 6, 2022
3289337
Added a link to the Netlify functions overview page to the readme
nickytonline May 6, 2022
9d0bf37
Updated deno settings for VS Code
nickytonline May 6, 2022
664de49
Added a link to the Netlify Edge functions overview page to the readme
nickytonline May 6, 2022
581ff1c
updated Netlify Edge function template files
nickytonline May 6, 2022
3ba4e09
Update templates/netlify/remix.init/remix.config-edge.js
nickytonline May 16, 2022
31d193c
Update templates/netlify/remix.init/index.js
nickytonline May 16, 2022
efcb3c4
Update packages/remix-netlify-edge/package.json
nickytonline May 16, 2022
8467d2d
created and now export the GetLoadContextFunction type
nickytonline May 16, 2022
99a8184
Update packages/remix-netlify-edge/remix-deno/crypto.ts
nickytonline May 16, 2022
3a3e6b1
chore: removed temporary remix-deno directory now that there is @remi…
nickytonline May 17, 2022
4ea0dad
missing types from server are now exported
nickytonline May 17, 2022
8ad0f6f
added missing new line
nickytonline May 17, 2022
ffc921d
denofying things
nickytonline May 19, 2022
07ee7f6
Update templates/netlify/remix.init/remix.env.d.ts
nickytonline May 19, 2022
b92e1ad
Update packages/remix-dev/compiler/plugins/serverBareModulesPlugin.ts
nickytonline May 19, 2022
d97eac3
remove tsconfig from netlify-edge template as Remix generates it
nickytonline May 19, 2022
11d0180
now imports are replaced for Edge when generating a Netlify Edge project
nickytonline May 19, 2022
52a1369
remix.init replaces @remix-run/netlify dep with @remix-run/netlify-ed…
nickytonline May 19, 2022
2fb574f
improved generating Netlify Edge project's package.json
nickytonline May 20, 2022
be0fe0b
small refactor
nickytonline Jun 17, 2022
efef1d4
Merge remote-tracking branch 'upstream/main' into nickytonline/chore-…
nickytonline Oct 27, 2022
25c0d7e
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Nov 8, 2022
b529737
chore: added missing @remix-run/deno@1.4.3 to fix yarn --frozen-lockfile
nickytonline Nov 8, 2022
d87302e
Update packages/remix-netlify-edge/server.ts
nickytonline Nov 9, 2022
91a524c
chore: no longer need the special case because the rollup config has …
nickytonline Nov 9, 2022
f81397f
chore: update to use published Netlify context type
ascorbic Nov 9, 2022
df0b43a
chore: reverted some spelling as it's not related to this PR
nickytonline Nov 9, 2022
994edc8
Merge branch 'nickytonline/chore-update-netlify-remix-template' into …
ascorbic Nov 9, 2022
dcba4e9
Merge pull request #1 from ascorbic/nickytonline/chore-update-netlify…
nickytonline Nov 9, 2022
099df65
chore: removed dev section from netlify.toml as Netlify's framework-i…
nickytonline Nov 9, 2022
0503e2e
chore: ran lint:fix
nickytonline Nov 9, 2022
c7b9802
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Nov 9, 2022
926255d
Update templates/netlify/remix.init/remix.config-edge.js
nickytonline Nov 10, 2022
fb6f12c
chore: move publish folder from public to build
nickytonline Nov 10, 2022
c3c1bff
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Nov 10, 2022
e4c0d6a
Revert "chore: move publish folder from public to build"
nickytonline Nov 10, 2022
3318f9f
chore: fixed file copy for Netlify Edge files
nickytonline Nov 11, 2022
1e3dcf6
chore: cleaned up generation of package.json for a Netlify edge project
nickytonline Nov 11, 2022
806fc74
chore: removed unused file
nickytonline Nov 11, 2022
2df3cc6
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Nov 11, 2022
86f1afd
chore: added process to globalThis as part of Node.js bridging
nickytonline Nov 11, 2022
0b542fe
chore: file isn't necessary with the assumption the Deno extension is…
nickytonline Nov 11, 2022
989d025
chore: moved setting of globalThis.process into @remix-run/netlify-edge
nickytonline Nov 11, 2022
10b2695
chore: switched from a regex/replace to good old replaceAll
nickytonline Nov 11, 2022
0d78477
chore: formatted .netlify gitignore entry like the CLI does
nickytonline Nov 11, 2022
803f9bf
chore: added netlify-edge for check for isDenoRuntime
nickytonline Nov 11, 2022
6195e51
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Nov 21, 2022
b37e849
chore: removed server.js and server.ts from VS Code settings for Netl…
nickytonline Nov 23, 2022
a96c7b7
chore: added generation of manifest for Netlify Edge to Remix compiler
nickytonline Nov 23, 2022
f69d22b
fix: fixed arguments for creating the Remix handler for Netlify Edge
nickytonline Nov 23, 2022
c9afffb
chore: bumped netlify-edge package.json to 1.7.6
nickytonline Nov 25, 2022
96e84b7
chore: cleanup based on PR feedback
nickytonline Nov 25, 2022
896a352
chore: cleanup based on PR feedback
nickytonline Nov 25, 2022
e2b59d6
chore: added some comments to global types for Netlify Edge template
nickytonline Nov 25, 2022
6037bb6
chore: cleanup based on PR feedback
nickytonline Nov 25, 2022
138c10c
chore: cleanup based on PR feedback
nickytonline Nov 25, 2022
aca10a1
test: added deployment setup for e2e tests for Netlify edge
nickytonline Nov 28, 2022
bb9e29f
chore: reverted replaceAlll to a regex to support Node 14
nickytonline Nov 28, 2022
48d76d5
chore: removed global types file from Netlify edge template as they'r…
nickytonline Nov 28, 2022
0a353f9
Update scripts/deployment-test/netlify.mjs
nickytonline Nov 28, 2022
0f64dbb
chore: update Netlify Edge deployment CI check
nickytonline Nov 29, 2022
8cf637e
chore: added @remix-run/deno to package.json for Netlify Edge template
nickytonline Nov 29, 2022
f81dbe0
chore: removed packages/remix-netlify-edge due to Deno style imports
nickytonline Nov 30, 2022
3196d98
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Nov 30, 2022
60fbe48
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Nov 30, 2022
6f1a643
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Nov 30, 2022
835a555
chore: used same convention as the rest of the project for .includes
nickytonline Nov 30, 2022
d87126e
chore: removed commented out configuration in root tsconfig
nickytonline Nov 30, 2022
b17d74a
fix: add type check for packages/remix-netlify-edge
nickytonline Nov 30, 2022
b708933
chore: fixed some imports for deno code
nickytonline Dec 1, 2022
31e16f2
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Dec 1, 2022
caae8bd
Update scripts/build.mjs
nickytonline Dec 1, 2022
4ddb5eb
Update .vscode/deno_resolve_npm_imports.json
nickytonline Dec 1, 2022
7a39ad4
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Dec 1, 2022
aed7635
Merge branch 'nickytonline/chore-update-netlify-remix-template' into …
nickytonline Dec 1, 2022
eaf50ff
Merge branch 'dev' into nickytonline/chore-update-netlify-remix-template
nickytonline Dec 1, 2022
02eb35b
wip
nickytonline Dec 2, 2022
147a2dc
Update packages/remix-deno/server.ts
nickytonline Dec 2, 2022
d44be97
Merge remote-tracking branch 'upstream/dev' into add-type-check
nickytonline Dec 2, 2022
7126e42
chore: removed old export for createRemixRequestHandler
nickytonline Dec 2, 2022
bf7c380
chore: bumped @remix-run/deno to 1.8.0
nickytonline Dec 2, 2022
fe69ed8
Merge pull request #2 from nickytonline/add-type-check
nickytonline Dec 2, 2022
b9a3029
trigger build
nickytonline Dec 2, 2022
ea2af16
chore: updated yarn.lock
nickytonline Dec 2, 2022
7fbc19e
chore: removed globals.d.ts from template file for vscode settings
nickytonline Dec 2, 2022
abf0ce9
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Dec 2, 2022
8f73241
Update packages/remix-netlify-edge/package.json
nickytonline Dec 2, 2022
cf1f398
Update packages/remix-netlify-edge/package.json
nickytonline Dec 2, 2022
c8c9fb4
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Dec 2, 2022
5db90ef
Update packages/remix-netlify-edge/package.json
nickytonline Dec 2, 2022
d96b4b3
Update packages/remix-netlify-edge/package.json
nickytonline Dec 2, 2022
49ea16e
chore: moved where export for createRemixRequestHandler happens
nickytonline Dec 2, 2022
d9c4634
chore: removed comment that is no longer valid
nickytonline Dec 2, 2022
a8dfa62
chore: fixed alphabetical ordering of imports
nickytonline Dec 2, 2022
9ed5799
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Dec 2, 2022
1166b49
chore: updated yarn.lock
nickytonline Dec 2, 2022
525f528
chore: fixed double dependency issue
nickytonline Dec 2, 2022
d5332f8
Revert "chore: fixed double dependency issue"
nickytonline Dec 2, 2022
a85cc3f
Merge branch 'dev' into nickytonline/chore-update-netlify-remix-template
nickytonline Dec 3, 2022
74d4c23
chore: removed @1.7.6 package references
nickytonline Dec 3, 2022
b384a5a
chore: minor formatting not required for this PR
nickytonline Dec 3, 2022
21f143d
Merge branch 'dev' into nickytonline/chore-update-netlify-remix-template
nickytonline Dec 6, 2022
d113f8a
chore: added @netlify/edge-functions to deps for Edge template
nickytonline Dec 6, 2022
1636c01
trigger build
nickytonline Dec 6, 2022
00cb7af
Merge branch 'dev' into nickytonline/chore-update-netlify-remix-template
nickytonline Dec 6, 2022
3ac5b9f
Merge branch 'dev' into nickytonline/chore-update-netlify-remix-template
nickytonline Dec 7, 2022
675ed9d
chore: removed netlify and netlify-edge server build targets and cust…
nickytonline Dec 8, 2022
f1e431f
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Dec 8, 2022
c896016
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Dec 8, 2022
7cdfca7
chore: updated yarn.lock
nickytonline Dec 8, 2022
36ae8d8
Update packages/remix-netlify-edge/server.ts
nickytonline Dec 9, 2022
90a9235
chore: using in-source config instead of manifest now
nickytonline Dec 9, 2022
c419ae8
chore: enabled edge functions caching
nickytonline Dec 9, 2022
8f1a6de
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Dec 19, 2022
d0e737e
chore: bumped @remix-run/deno version in nextlify edge package
nickytonline Dec 19, 2022
bc672cc
chore: bumped @remix-run/netlify-edge to 1.9.0
nickytonline Dec 19, 2022
a8c1db1
chore: udpated yarn.lock
nickytonline Dec 19, 2022
212aecf
Put back Netlify Functions server build target
nickytonline Dec 19, 2022
adb9639
chore: added "skipLibCheck": true, like the other templates to avoid …
nickytonline Dec 20, 2022
0346753
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Dec 20, 2022
733559f
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Dec 22, 2022
29763da
chore: put back netlify-edge server build target
nickytonline Dec 22, 2022
cef75f1
Merge remote-tracking branch 'upstream/dev' into nickytonline/chore-u…
nickytonline Jan 16, 2023
b82df5c
chore: bumped remix-netlify-edge package version
nickytonline Jan 16, 2023
e136beb
chore: insource config is an object now
nickytonline Jan 16, 2023
f96d44f
chore: updated yarn lock file
nickytonline Jan 16, 2023
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
37 changes: 37 additions & 0 deletions .github/workflows/deployments.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,43 @@ jobs:
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.TEST_NETLIFY_TOKEN }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should also add it here for consistency?

Suggested change
NETLIFY_AUTH_TOKEN: ${{ secrets.TEST_NETLIFY_TOKEN }}
NETLIFY_AUTH_TOKEN: ${{ secrets.TEST_NETLIFY_TOKEN }}
USE_NETLIFY_EDGE: false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want it here because it's literally only for CI for Netlify Edge. Just the presence of the environment variable is all we care about which is why I didn't include it for the Netlify Functions deploy.

Copy link
Member

@MichaelDeBoey MichaelDeBoey Nov 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our integration tests are also rewritten to be explicit about both cases, so that's why I think we should do it here as well

test.use({ javaScriptEnabled: true }); // explicitly set so we don't have to check against undefined

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

env vars are strings, so you'd need to change the comparison logic if you do this, because "false" is truthy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wouldn't be a problem to do the following check imo

if(USE_NETLIFY_EDGE === "true") {
  // ...
}


netlify_edge_deploy:
name: "Netlify Edge Deploy"
if: github.repository == 'remix-run/remix'
runs-on: ubuntu-latest
steps:
- name: πŸ›‘ Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.11.0

- name: ⬇️ Checkout repo
uses: actions/checkout@v3

- name: βŽ” Setup node
uses: actions/setup-node@v3
with:
node-version-file: ".nvmrc"
cache: npm
cache-dependency-path: ./scripts/deployment-test/package.json # no lockfile, key caching off package.json

# some deployment targets require the latest version of npm
# TODO: remove this eventually when the default version we get
# is "latest" enough.
- name: πŸ“¦ Install latest version of npm
run: npm install -g npm@latest
working-directory: ./scripts/deployment-test

- name: πŸ“₯ Install deployment-test deps
run: npm install
working-directory: ./scripts/deployment-test

- name: πŸš€ Deploy to Netlify
run: node ./netlify.mjs
working-directory: ./scripts/deployment-test
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.TEST_NETLIFY_TOKEN }}
# required so we can skip the prompt in remix.init
USE_NETLIFY_EDGE: 1

vercel_deploy:
name: "Vercel Deploy"
if: github.repository == 'remix-run/remix'
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ tsconfig.tsbuildinfo
/scripts/playground/template.local
/scripts/playground/template/build
/scripts/playground/template/package-lock.json

# Local Netlify folder
.netlify
3 changes: 3 additions & 0 deletions .vscode/deno_resolve_npm_imports.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"imports": {
"@remix-run/server-runtime": "https://esm.sh/@remix-run/server-runtime@nightly",
"@remix-run/deno": "../packages/remix-deno/index.ts",
"@remix-run/deno/": "../packages/remix-deno/",
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
"@netlify/edge-functions": "https://edge.netlify.com",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add these to the template's .vscode/resolve_npm_imports.json as well? πŸ€”

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they're compiled away by then

Copy link
Member

@MichaelDeBoey MichaelDeBoey Dec 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since @remix-run/netlify-edge isn't compiled, these ones aren't compiled as well, so we should include them in the template's .vscode/resolve_npm_imports.json, just like we do for the Deno template I guess?

We also include it in the template once #3104 (comment) is fixed

CC/ @pcattori

Copy link
Contributor

@ascorbic ascorbic Dec 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@remix-run/netlify-edge is compiled. It isn't typechecked, but the actual package is compiled with rollup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Deno supports TS, I think we don't need to compile @remix-run/netlify-edge either, just like we don't compile @remix-run/deno πŸ€”

@pcattori can probably give more insights in what's best here.

Copy link
Contributor

@ascorbic ascorbic Dec 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's imported by code that's then compiled by node and rollup. Using an import map won't make any difference, because rollup will ignore it and just load and bundle @remix-run/deno, @netlify/edge-functions and @remix-run/server-runtime from node_modules anyway. The Remix compiler always compiles the site's server.ts uisng rollup, so even if it's using imports that are Deno-friendly, that's misleading because it's all compiled into a bundled server.js file before Deno ever runs it. The exception is URL imports, which rollup ignores and passes-through unchanged in the generated code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't compile @remix-run/deno for better IDE support, but @pcattori can probably give more insights into the reasoning behind it.

Whatever we decide to do, I think we should streamline it in both the deno & netlify-edge template though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just trying to see the benefit here. I have it working here, https://github.com/nickytonline/remix/pull/3. Even though our edge function offering uses Deno, the projects people create, at least at the moment are always Node.js projects. We don’t have a deno.json and our tooling is Node.js centric, i,e, the Netlify CLI. As well, they may add Netlify functions for certain things, e,g, scheduled functions, background functions or even a custom function, and these are Node.js based.

"mime": "https://esm.sh/mime@3.0.0"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"packages/remix-eslint-config",
"packages/remix-express",
"packages/remix-netlify",
"packages/remix-netlify-edge",
"packages/remix-node",
"packages/remix-react",
"packages/remix-serve",
Expand Down
1 change: 1 addition & 0 deletions packages/remix-deno/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {
} from "./implementations.ts";

export {
createRequestHandler as createRemixRequestHandler,
createSession,
defer,
isCookie,
Expand Down
4 changes: 3 additions & 1 deletion packages/remix-dev/compiler/compilerServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ const createEsbuildConfig = (
let isCloudflareRuntime = ["cloudflare-pages", "cloudflare-workers"].includes(
config.serverBuildTarget ?? ""
);
let isDenoRuntime = config.serverBuildTarget === "deno";
let isDenoRuntime = ["deno", "netlify-edge"].includes(
config.serverBuildTarget ?? ""
);

let { mode } = options;
let { rootDirectory } = config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export function serverBareModulesPlugin(
remixConfig: RemixConfig,
onWarning?: (warning: string, key: string) => void
): Plugin {
let isDenoRuntime = remixConfig.serverBuildTarget === "deno";
let isDenoRuntime = ["deno", "netlify-edge"].includes(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacob-ebey Can you please enlighten me about why this code is here? Why don't we want to use tsconfig to resolve module paths on Deno? And is this applicable to Netlify Edge as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deno essentially forks typescript and has their own module resolution algorithm. Using the standard tsconfig won't suffice as it doesn't account for URL imports. When building for deno, we add an esbuild plugin that uses the deno resolution algorithm.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gee, thanks Deno! In that case it sounds like maybe we need another top-level config option (since serverBuildTarget is going away) that controls how we resolve modules... maybe something like:

moduleResolution: "node" | "tsconfig" | "deno"

Does that sound reasonable?

remixConfig.serverBuildTarget ?? ""
);

// Resolve paths according to tsconfig paths property
let matchPath = isDenoRuntime
Expand Down Expand Up @@ -108,6 +110,7 @@ export function serverBareModulesPlugin(
case "cloudflare-pages":
case "cloudflare-workers":
case "deno":
case "netlify-edge":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding this line to our compiler, use serverDependenciesToBundle in your template config to automatically bundle everything.

return undefined;
}

Expand Down
5 changes: 5 additions & 0 deletions packages/remix-dev/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type ServerBuildTarget =
| "node-cjs"
| "arc"
| "netlify"
nickytonline marked this conversation as resolved.
Show resolved Hide resolved
| "netlify-edge"
| "vercel"
| "cloudflare-pages"
| "cloudflare-workers"
Expand Down Expand Up @@ -342,6 +343,7 @@ export async function readConfig(
case "cloudflare-pages":
case "cloudflare-workers":
case "deno":
case "netlify-edge":
serverModuleFormat = "esm";
serverPlatform = "neutral";
break;
Expand Down Expand Up @@ -380,6 +382,9 @@ export async function readConfig(
case "netlify":
serverBuildPath = ".netlify/functions-internal/server.js";
break;
case "netlify-edge":
serverBuildPath = ".netlify/edge-functions/server.js";
break;
case "vercel":
serverBuildPath = "api/index.js";
break;
Expand Down
13 changes: 13 additions & 0 deletions packages/remix-netlify-edge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Welcome to Remix!

[Remix](https://remix.run) is a web framework that helps you build better websites with React.

To get started, open a new shell and run:

```sh
npx create-remix@latest
```

Then follow the prompts you see in your terminal.

For more information about Remix, [visit remix.run](https://remix.run)!
4 changes: 4 additions & 0 deletions packages/remix-netlify-edge/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import "@remix-run/deno/globals.ts";

// @ts-ignore
globalThis.process ||= { env: Deno.env.toObject() };
4 changes: 4 additions & 0 deletions packages/remix-netlify-edge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import "./globals.ts";

export type { GetLoadContextFunction, RequestHandler } from "./server.ts";
export { createRequestHandler } from "./server.ts";
34 changes: 34 additions & 0 deletions packages/remix-netlify-edge/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@remix-run/netlify-edge",
"description": "Netlify Edge server request handler for Remix",
"version": "1.10.1",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/remix-run/remix",
"directory": "packages/remix-netlify-edge"
},
"bugs": {
"url": "https://github.com/remix-run/remix/issues"
},
"dependencies": {
"@remix-run/deno": "1.10.1"
},
"devDependencies": {
"@netlify/edge-functions": "^2.0.0"
nickytonline marked this conversation as resolved.
Show resolved Hide resolved
},
"peerDependencies": {
"@netlify/edge-functions": "^2.0.0"
},
"engines": {
"node": ">=14"
},
"files": [
"dist/",
"CHANGELOG.md",
"LICENSE.md",
"README.md"
]
}
6 changes: 6 additions & 0 deletions packages/remix-netlify-edge/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { getAdapterConfig } = require("../../rollup.utils");

/** @returns {import("rollup").RollupOptions[]} */
module.exports = function rollup() {
return [getAdapterConfig("netlify-edge")];
};
66 changes: 66 additions & 0 deletions packages/remix-netlify-edge/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { createRemixRequestHandler } from "@remix-run/deno";
import type { AppLoadContext, ServerBuild } from "@remix-run/deno";
import type { Context } from "@netlify/edge-functions";

type LoadContext = AppLoadContext & Context;

/**
* A function that returns the value to use as `context` in route `loader` and
* `action` functions.
*
* You can think of this as an escape hatch that allows you to pass
* environment/platform-specific values through to your loader/action.
*/
export type GetLoadContextFunction = (
request: Request,
context: Context
) => Promise<LoadContext> | LoadContext;

export type RequestHandler = (
request: Request,
context: LoadContext
) => Promise<Response | void>;

export function createRequestHandler({
build,
mode,
getLoadContext,
}: {
build: ServerBuild;
mode?: string;
getLoadContext?: GetLoadContextFunction;
}): RequestHandler {
let remixHandler = createRemixRequestHandler(build, mode);

let assetPath = build.assets.url.split("/").slice(0, -1).join("/");

return async (
request: Request,
context: LoadContext
): Promise<Response | void> => {
let { pathname } = new URL(request.url);
// Skip the handler for static files
if (pathname.startsWith(`${assetPath}/`)) {
return;
}
try {
let loadContext = (await getLoadContext?.(request, context)) || context;

let response = await remixHandler(request, loadContext);
if (response.status === 404) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just include a separate createRequestHandlerWithStaticFiles like we do in our deno adapter?

// Check if there is a matching static file
let originResponse = await context.next({
sendConditionalRequest: true,
});
if (originResponse.status !== 404) {
return originResponse;
}
}
return response;
} catch (error: unknown) {
console.error(error);

return new Response("Internal Error", { status: 500 });
}
};
}
17 changes: 17 additions & 0 deletions packages/remix-netlify-edge/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"include": ["**/*.ts"],
"compilerOptions": {
"lib": ["ES2019", "WebWorker"],
"target": "ES2019",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"strict": true,
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "../../build/node_modules/@remix-run/netlify-edge",
"rootDir": ".",
// Avoid naming conflicts between history and react-router-dom relying on
// lib.dom.d.ts Window and this being a WebWorker env.
"skipLibCheck": true
}
}
2 changes: 1 addition & 1 deletion rollup.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ module.exports = {

/**
* @typedef {Record<string, { values?: string[]; types?: string[] }>} MagicExports
* @typedef {"architect" | "cloudflare-pages" | "cloudflare-workers" | "express" | "netlify" | "vercel"} RemixAdapter
* @typedef {"architect" | "cloudflare-pages" | "cloudflare-workers" | "express" | "netlify" | "netlify-edge" | "vercel"} RemixAdapter
* @typedef {"cloudflare" | "node" | "deno"} RemixRuntime
* @typedef {`@remix-run/${RemixAdapter | RemixRuntime | "dev" | "eslint-config" | "react" | "serve" | "server-runtime"}`} ScopedRemixPackage
* @typedef {"create-remix" | "remix" | ScopedRemixPackage} RemixPackage
Expand Down
9 changes: 9 additions & 0 deletions scripts/deployment-test/netlify.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fse from "fs-extra";
import { createApp } from "@remix-run/dev";
import PackageJson from "@npmcli/package-json";

import netlifyRemixInit from "../../templates/netlify/remix.init/index.js";
import {
addCypress,
CYPRESS_CONFIG,
Expand All @@ -16,6 +17,8 @@ import {
validatePackageVersions,
} from "./_shared.mjs";

const { USE_NETLIFY_EDGE } = process.env;

let APP_NAME = getAppName("netlify");
let PROJECT_DIR = getAppDirectory(APP_NAME);
let CYPRESS_DEV_URL = "http://localhost:3000";
Expand All @@ -27,6 +30,12 @@ async function createNewApp() {
useTypeScript: true,
projectDir: PROJECT_DIR,
});

// In conjunction with USE_NETLIFY_EDGE we explicitly call remix init
// to create the edge version of the app
if (USE_NETLIFY_EDGE) {
await netlifyRemixInit({ rootDirectory: PROJECT_DIR });
}
}

let client = new NetlifyAPI(process.env.NETLIFY_AUTH_TOKEN);
Expand Down
1 change: 1 addition & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let remixPackages = {
"cloudflare-pages",
"cloudflare-workers",
"express",
"netlify-edge",
"netlify",
"vercel",
],
Expand Down
4 changes: 3 additions & 1 deletion templates/netlify/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ node_modules

/.cache
/public/build
/.netlify
.env

# Local Netlify folder
.netlify
6 changes: 3 additions & 3 deletions templates/netlify/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Welcome to Remix!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This template should be a repo somewhere on the Netlify GH org. This templates dir is a temporary spot for legacy v1 templates. No new templates should be created here.


- [Remix Docs](https://remix.run/docs)
- [Netlify Functions](https://www.netlify.com/products/functions/)
- [Netlify Functions Overview](https://docs.netlify.com/functions/overview)

## Netlify Setup

Expand Down Expand Up @@ -34,7 +34,7 @@ netlify init
The Remix dev server starts your app in development mode, rebuilding assets on file changes. To start the Remix dev server:

```sh
npm run dev
netlify dev
```

Open up [http://localhost:3000](http://localhost:3000), and you should be ready to go!
Expand All @@ -51,7 +51,7 @@ Note: When running the Netlify CLI, file changes will rebuild assets, but you wi

## Deployment

There are two ways to deploy your app to Netlify, you can either link your app to your git repo and have it auto deploy changes to Netlify, or you can deploy your app manually. If you've followed the setup instructions already, all you need to do is run this:
There are two ways to deploy your app to Netlify, you can either link your app to your git repository and have it auto deploy changes to Netlify, or you can deploy your app manually. If you've followed the setup instructions already, all you need to do is run this:

```sh
# preview deployment
Expand Down
21 changes: 9 additions & 12 deletions templates/netlify/netlify.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
[build]
command = "remix build"
publish = "public"

[dev]
command = "remix watch"
port = 3000
command = "remix build"
publish = "public"

[[redirects]]
from = "/*"
to = "/.netlify/functions/server"
status = 200
from = "/*"
to = "/.netlify/functions/server"
status = 200

[[headers]]
for = "/build/*"
[headers.values]
"Cache-Control" = "public, max-age=31536000, s-maxage=31536000"
for = "/build/*"

[headers.values]
"Cache-Control" = "public, max-age=31536000, s-maxage=31536000"
Loading