Skip to content

fix: support npm run dev (remix dev) and Netlify CLI #95

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

Merged
merged 11 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ node_modules

/.cache
/public/build
/build
Copy link
Contributor Author

Choose a reason for hiding this comment

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

remix dev generates these artifacts as well as those in /public/build

.env

# Local Netlify folder
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"build": "remix build",
"dev": "remix dev",
"start": "cross-env NODE_ENV=production netlify dev",
"start": "netlify serve",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using ntl serve as it makes more sense in this context.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd normally expect start to run dev, but I guess it could go either way. Adding a serve command could be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was going with your suggestion from the other day unless I was dreaming. 🙃 . Using serve avoids running build explicitly which is nice and ntl dev still gives us dev mode

Copy link
Contributor

Choose a reason for hiding this comment

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

What I meant was that if people wanted the previous behaviour (i.e. running the prod server) then they could use ntl serve instead

"typecheck": "tsc -b"
},
"dependencies": {
Expand Down
23 changes: 14 additions & 9 deletions remix.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const baseConfig =
process.env.NODE_ENV === "production"
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'll only pull in the custom server settings if building for production, i.e. ntl serve, ntl build,ntl deploy or via git push.

? // when running the Netify CLI or building on Netlify, we want to use
{
server: "./server.js",
serverBuildPath: ".netlify/functions-internal/server.js",
}
: // otherwise support running remix dev, i.e. no custom server
undefined;

/** @type {import('@remix-run/dev').AppConfig} */
module.exports = {
serverBuildTarget: "netlify",
server:
process.env.NETLIFY || process.env.NETLIFY_LOCAL
? "./server.js"
: undefined,
...baseConfig,
ignoredRouteFiles: ["**/.*"],
// appDirectory: "app",
// assetsBuildDirectory: "public/build",
// serverBuildPath: ".netlify/functions-internal/server.js",
// publicPath: "/build/",
// add your own custom config here if you want to.
//
// See https://remix.run/docs/en/v1/file-conventions/remix-config
};
20 changes: 16 additions & 4 deletions remix.init/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,27 @@ Ensure all packages are installed by running:
npm install
```

The Netlify CLI starts your app in development mode, rebuilding assets on file changes.
Run

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

Open up [http://localhost:3000](http://localhost:3000), and you should be ready to go!
Open up [http://localhost:8888](http://localhost:8888), and you're ready to go!

Note: When running the Netlify CLI, file changes will rebuild assets, but you will not see the changes to the page you are on unless you do a browser refresh of the page. Due to how the Netlify CLI builds the Remix App Server, it does not support hot module reloading.
### Adding Redirects and Rewrites

To add redirects and rewrites, add them to the `netlify.toml` file or to the [\_app_redirects](_app_redirects) file. For more information about redirects and rewrites, see the [Netlify docs](https://docs.netlify.com/routing/redirects/).

### Serve your site locally

Run

```sh
npm run start
```

to serve your site locally at [http://localhost:8888](http://localhost:8888).

## Deployment

Expand Down
7 changes: 7 additions & 0 deletions remix.init/_app_redirects
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This template uses this file instead of the typicial Netlify _redirects file.
# For more information about redirects and rewrites, see https://docs.netlify.com/routing/redirects/.

# Do not remove the line below. This is required to serve the site when deployed.
/\* /.netlify/functions/server 200

# Add other redirects and rewrites here and/or in your netlify.toml
6 changes: 5 additions & 1 deletion remix.init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const edgeFilesToCopy = [
];

// Netlify Functions template file changes
const filesToCopy = [["README.md"], ["netlify-toml", "netlify.toml"]];
const filesToCopy = [
["README.md"],
["netlify-toml", "netlify.toml"],
["_app_redirects"],
];

const tsExtensionMatcher = /\.ts(x?)$/;

Expand Down
9 changes: 4 additions & 5 deletions remix.init/netlify-toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
[build]
command = "remix build"
command = "remix build && cp _app_redirects public/_redirects"
publish = "public"

[[redirects]]
from = "/*"
to = "/.netlify/functions/server"
status = 200
[dev]
command = "npm run dev"
targetPort = 3000

[[headers]]
for = "/build/*"
Expand Down