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

Inconsistency between dev and build with trailing slash #4638

Closed
1 task
AidanNichol opened this issue Sep 6, 2022 · 5 comments
Closed
1 task

Inconsistency between dev and build with trailing slash #4638

AidanNichol opened this issue Sep 6, 2022 · 5 comments

Comments

@AidanNichol
Copy link

What version of astro are you using?

1.1.5

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

none

What package manager are you using?

yarn

What operating system are you using?

Mac

Describe the Bug

working with astro 1.1.5

let pathname = new URL(Astro.request.url).pathname;

with astro dev this doesn't return a trailing '/' whereas astro build does.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-rc3xnz?file=src%2Fpages%2Findex.astro,src%2Fpages%2Fbug.astro,package.json&on=stackblitz

Participation

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

matthewp commented Sep 6, 2022

You want to use trailingSlash: never in order to control trailing slash behavior. By default it is set to ignore which means it will accept both in dev mode. In the build it can obviously only be one of the two and it defaults to including the trailing slash when using build.format = 'directory'.

@matthewp matthewp closed this as completed Sep 6, 2022
@fabien
Copy link

fabien commented Oct 5, 2022

@matthewp I ran into this issue as well. Adding trailingSlash: never to my config did not help, the trailing slash was still there when using Astro.url.pathname (in a static site build).

@underblob
Copy link

underblob commented Oct 19, 2022

I think this is not obvious to the uninitiated. I spent longer than I would have liked trying to figure it out. When trying to identify the current page from the Astro.glob collection:

const posts = await Astro.glob('../pages/blog/*');
const currIndex = posts.findIndex(o => o.url === Astro.url.pathname);
const prev = posts[currIndex - 1];
const next = posts[currIndex + 1];

...I thought it was an Astro.glob behavior issue for a long time until I printed Astro.url.pathname to the render in both development and production and saw the trailing slash. It would be friendlier if the default values across dev/build were consistent.

@tylerwray
Copy link

I would love to re-open this issue and work on it.

The issue I'm having is that for a production build there is no way to have Astro.url remove the trailing slash.

I'm doing this regex workaround now to conditionally remove a trailing slash

const pathname = Astro.url.pathname.replace(/\/$/, "")

It would be great if there was a way to make production builds not include trailing slashes if that's someone's preference.

@Adrianjs42
Copy link

I would love to re-open this issue and work on it.

The issue I'm having is that for a production build there is no way to have Astro.url remove the trailing slash.

I'm doing this regex workaround now to conditionally remove a trailing slash

const pathname = Astro.url.pathname.replace(/\/$/, "")

It would be great if there was a way to make production builds not include trailing slashes if that's someone's preference.

@tylerwray that would be great. I am also still looking for a solution to the issue #5349

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

6 participants