Preact-cli: URL with period doesn't work when using "npm start" (serve)

Created on 30 Jan 2020  路  10Comments  路  Source: preactjs/preact-cli

Works: https://cli-demo-next.preactjs.com/profile/john.txt

Doesn't work: http://127.0.0.1:8080/profile/john.txt

Error message: Cannot GET /profile/john.txt

Local is a fresh install of node and all the needed libraries.

Good first bug enhancement has-fix has-workaround help wanted

Most helpful comment

Python's simple server doesn't work for Single Page Apps.

Currently we use serve, which treats any URL with a file extension as being a path on disk. Instead, we could switch to sirv-cli:

npx sirv-cli build -Dcs
# or
npm i -D sirv-cli && ./node_modules/.bin/sirv build -Dcs

Sirv maintains an in-memory file list and doesn't need to rely on URL patterns to trigger single-page routing fallbacks. It defaults to localhost:5000 --host foo --port 8080 to change. You can put this in an npm script, or run it from your teminal.

@prateekbh @ForsakenHarmony - thinking we should change the default template to use sirv (here).

All 10 comments

hmmm https://cli-demo-next.preactjs.com is configured as a SPA, which means GET requests to all routes would be transferred to /. Something tells me that the same is not true for your local environment

Works fine without the period: http://127.0.0.1:8080/profile/john

I used npm run start to run it locally as instructed by the preact create command and the README.md file it generated, I also tried python3 -m http.server in the build directory after building with the same result.

In any case I need URLs with periods in them, while trying to diagnose the issue I noticed that cli-demo-next.preactjs.com supports them.

How do I configure my local environment to get the desired functionality?

Just realized that python3 -m http.server wouldn't work for any URL other than root, don't know why I expected otherwise.

Still, I don't see why npm run start wouldn't work, especially if it works for other non-root URLs.

Additionally, following a link containing a period from within the loaded site correctly changes the URL and loads the appropriate page, but upon refreshing it gives that error.

How do I configure my local environment to get the desired functionality?

A simple HTTP-server might not be able to do this. A more elaborate server can implement a catch-all handler and serve index.html to it.

Still, I don't see why npm run start wouldn't work, especially if it works for other non-root URLs.

npm run start would only work for root index.html and pre-rendered urls. Are you pre-rendering any routes?

any repo to reproduce this?

I am using the project that preact create default my-project creates with no modification.

I do not know if I am pre-rendering any routes, I have not changed any settings from their defaults.

Will check

I just checked, seems to be working fine

My setup for trying this out on local

  1. use https://www.npmjs.com/package/http-server
  2. rename index.html to 404.html inside build folder, so all requests can be directed to it

Python's simple server doesn't work for Single Page Apps.

Currently we use serve, which treats any URL with a file extension as being a path on disk. Instead, we could switch to sirv-cli:

npx sirv-cli build -Dcs
# or
npm i -D sirv-cli && ./node_modules/.bin/sirv build -Dcs

Sirv maintains an in-memory file list and doesn't need to rely on URL patterns to trigger single-page routing fallbacks. It defaults to localhost:5000 --host foo --port 8080 to change. You can put this in an npm script, or run it from your teminal.

@prateekbh @ForsakenHarmony - thinking we should change the default template to use sirv (here).

Opened a PR against the default template (https://github.com/preactjs-templates/default/pull/39)

Should be fixed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oren picture oren  路  3Comments

AlStar01 picture AlStar01  路  3Comments

andybons picture andybons  路  3Comments

scottmas picture scottmas  路  3Comments

nephix picture nephix  路  4Comments