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

Base path not working like the docs said #2140

Closed
1 task
harrytran998 opened this issue Dec 2, 2022 · 11 comments
Closed
1 task

Base path not working like the docs said #2140

harrytran998 opened this issue Dec 2, 2022 · 11 comments

Comments

@harrytran998
Copy link

harrytran998 commented Dec 2, 2022

What version of astro are you using?

latest

Are you using an SSR adapter? If so, which one?

NodeJs 18 LTS + SSR nodejs standalone

What package manager are you using?

yarn 3

What operating system are you using?

Mac

Describe the Bug

https://docs.astro.build/en/reference/configuration-reference/#base

The docs said it only affects production. But seem in development, this still have effect.

When I config astro.config.js like this

import { defineConfig } from 'astro/config';
import svelte from '@astrojs/svelte';
import AstroNode from '@astrojs/node';

export default defineConfig({
  output: 'server',
  integrations: [svelte()],
  base: '/ok',
  server: {
    port: 4000,
    host: '0.0.0.0',
  },
  adapter: AstroNode({
    mode: 'standalone',
  }),
});

In dev mode, it will throw a warning like this

[serve]  404   /favicon.ico
[dev] Requests for items in your public folder must also include your base. /favicon.svg should be /ok/favicon.svg. Omitting the base will break in production.

And the screen shows like this

404: Not found
In your site you have your base path set to [/ok/](http://localhost:3000/ok/). Do you want to go there instead?

In my mind, I think this base path is like the concept of NextJs. But seem not correct like they do.

--- With NextJs concept
When I config basePath to /ok --> In production mode, at least this will auto append /ok to the href with next/link. And the assets must be appended with base path ok/.

But seem Astro not auto append href 🤔.

Link to Minimal Reproducible Example

Stackblitz did not work correctly with the base path. So your team needs to pull this source code 😆.

https://github.com/harrytran998/rep-astro-base-path
(Here is the version of stackblitz to check https://stackblitz.com/edit/github-ncwvvs?file=astro.config.mjs)

Participation

  • I am willing to submit a pull request for this issue.
@marcfielding1
Copy link

marcfielding1 commented Dec 3, 2022

I had this issue, and did this in the build script as a workaround, the problem is setting basepath doesn't affect where Astro puts the build assets:

So make your base path directory in dist/ then copy/move the files from dist to the correct place.

    "build": "astro build && mkdir ./ok && cp -R ./dist/* ./ok && mv ./ok ./dist",

Then have your hrefs and src's point at /ok/theAsset.png/js etc

I spoke to Nate on their discord about this he was enquiring on if this was intended behavior.

@harrytran998
Copy link
Author

I had this issue, and did this in the build script as a workaround, the problem is setting basepath doesn't affect where Astro puts the build assets:

So make your base path directory in dist/ then copy/move the files from dist to the correct place.

    "build": "astro build && mkdir ./ok && cp -R ./dist/* ./ok && mv ./ok ./dist",

Then have your hrefs and src's point at /ok/theAsset.png/js etc

I spoke to Nate on their discord about this he was enquiring on if this was intended behavior.

Thanks for your suggestion 🙏. But seem we need official support for everyone can use this 😁.
I will try this soon.

@wassfila
Copy link
Contributor

wassfila commented Dec 3, 2022

this feels like a painful issue, every Astro user has a different adhoc solution, here's what I ended up doing, a custom config file

if(process.env.MODE == "MIDDLEWARE"){
    config.base = ""
}

https://github.com/MicroWebStacks/astro-big-doc/blob/dd2ef27ff03fa649f801f66e7e96a5e7334513b6/config.js#L13

because if I try to import the config in astro.config.mjs, then I get a real dev server crash, mystery, closed it only because it's vite, not Astro, but issue still valid posted details here withastro/astro#5517

@marcfielding1
Copy link

marcfielding1 commented Dec 3, 2022

Yeah I think there needs be a cross-platform solution to how basePath is applied - but this open source so we can all find a solution and contribute rather than relying on the core team @wassfila my company are happy to work on a proposal for this in terms of functionality but the trouble we'd need to deal with the various adaptors I think.(vercel, upcoming AWS, netfly etc) so any core team input on what the preferred direction is would help before we do the work.

@wassfila
Copy link
Contributor

wassfila commented Dec 3, 2022

That's true @marcfielding1 , especially for improvements that break compatibility. For base and many others, Astro inherites Vites problems, see https://vitejs.dev/config/shared-options.html#base
So maybe looking forward on how Vite is evolving can help. Astro is a Vite user but also contributor.

@marcfielding1
Copy link

basePath seems wonky at the moment but without being involved in internal astro dev conversation I can't criticise really, so yeah lets see what the team come back with - it definitely needs work, just in line with the goals I guess.

@harrytran998
Copy link
Author

harrytran998 commented Dec 6, 2022

LOL, seems not to work for me in my case 😆. Bcs we have a web with the old path /prefix/ok/* --> Astro project will serve ok/*.

Currently, I use the scripts to replace the path in entry.mjs build file to work around but looks so messy 😆. I hope the Astro team can investigate some time to do this.

@matthewp
Copy link
Contributor

matthewp commented Dec 6, 2022

Moving to the docs repo since this is a docs issue. Using the base in development is intended behavior.

@matthewp matthewp transferred this issue from withastro/astro Dec 6, 2022
@sarah11918
Copy link
Member

I think this is related to the rabbit hole of PRs that started here: #1643 that I am trying to pin down! I suspect we actually have proper docs that were blocked by some unexpected behaviour. I think we're unblocked, and I'm trying to confirm whether we can now re-merge a previous docs fix that we intended to have! 😅

@sarah11918
Copy link
Member

And, looks like I should be able to close this! Although this previous PR to our configuration reference was merged, it was reverted to fix a bug with the intention we'd add it back after bug fix. Bug is fixed, and we'll be re-introducing this into docs: https://github.com/withastro/astro/pull/4954/files

If you notice any problems, feel free to reopen!

@sarah11918
Copy link
Member

Followup: withastro/astro#5693 has been made to address this. Comments can be made there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants