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

catch ERR_INVALID_URL #176

Merged
merged 2 commits into from
Nov 15, 2023
Merged
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
4 changes: 2 additions & 2 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class Server {

_handleRequest: RequestListener = async (req, res) => {
console.log(req.method, req.url);
const url = new URL(req.url!, "http://localhost");
let {pathname} = url;
try {
const url = new URL(req.url!, "http://localhost");
let {pathname} = url;
Copy link
Contributor

Choose a reason for hiding this comment

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

should we just clean the req.url so it doesn't end with 2 backslashes? or redirect? I think in this case it's ok to assume that it was just a typo

Copy link
Contributor Author

@Fil Fil Nov 15, 2023

Choose a reason for hiding this comment

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

Suppose you have a link in index.md to the "//" URL; now the dev server returns a 404 instead of crashing, which is what this PR wanted.

We could decide to redirect // to / on the dev server, but it would only hide this problem from the developer, as the link wouldn't work on the build. So my take is that it is better to be strict and error. (But more discussions on what to do on 404s are happening at #174 :) )

if (pathname === "/_observablehq/runtime.js") {
send(req, "/@observablehq/runtime/dist/runtime.js", {root: "./node_modules"}).pipe(res);
} else if (pathname.startsWith("/_observablehq/")) {
Expand Down