Replies: 5 comments 11 replies
-
+1 to anything in this area to make it more clear. I remember this was an area I struggled with when first getting started with Astro. Every time I pick up a different Astro project I find I need to re-learn these bits as well because they aren't 100% clear. Probably not in the scope of this, but having a way to make relative links more easily useable would be a win. I often see we suggest to people to use a full URL with a beginning slash because this area is difficult to explain and troubleshoot. This is honestly more of an issue with HTML links not being the same as file system links, but any way that Astro could make this easier for end users would be appreciated. |
Beta Was this translation helpful? Give feedback.
-
Linking a helpful guide to different deployment platforms: https://github.com/slorber/trailing-slash-guide Noting also that Starlight uses |
Beta Was this translation helpful? Give feedback.
-
From the PR: withastro/astro#11922 My initial thought is that pushing @bluwy said in reply:
Which web server doesn't allow this? I just tested with |
Beta Was this translation helpful? Give feedback.
-
Proposed default: trailingSlash: {
page: 'always',
endpoint: 'ignore'
} trailingSlash: 'never' // shortcut for..
trailingSlash: {
page: 'never',
endpoint: 'never'
} |
Beta Was this translation helpful? Give feedback.
-
I also think the behavior of |
Beta Was this translation helpful? Give feedback.
-
Body
Summary
Make
build.format
default value as"preserve"
and mergebuild.format
intotrailingSlash
.Note
17 Jan 2025: I've made some significant updates to the proposed changes since the last update. Instead of having
trailingSlash
apply to pages and endpoints. The changes now reflects individual options for configuring trailing slashes for pages and endpoints instead. This was changed as pages and endpoints have different ideal setups regarding trailing slashes.Background & Motivation
build.format
andtrailingSlash
are closely related. The way you build your pages (as files or directories) affect whether you need a trailing slash to access them or not.file.html
can only be accessed fromexample.com/file
dir/index.html
can only be accessed fromexample.com/dir/
The slashes are important as they affect how relative links are resolved on the webpage. You can't force
file.html
to be accessed fromexample.com/file/
for example without breaking relative links.Tricky workaround
Technically above can be kinda solved if you generate relative links with the anticipation of accessing from
example.com/file/
, however it can go wrong in, e.g. SSG, where deployment platforms may not allow you to accessfile.html
this way in the first place, you need SSR (as a custom server) to help with this.We try to avoid users hitting this footgun by suggesting a pair of
build.format
andtrailingSlash
configs: https://docs.astro.build/en/reference/configuration-reference/#effect-on-astrourl. The sour thing however is that our default options for both are already conflicting.Pages vs endpoints
It's also worth sheding some light about how trailing slashes affects pages vs endpoints. Pages are files that emit HTML, while endpoints are usually server handlers that respond to dynamic requests. If endpoints are prerendered, then their results are written to the file as is (e.g.
src/pages/dir/foo.ts
->dist/dir/foo
,src/pages/dir/data.json
->dist/dir/data.json
).Trailing slashes often only matter for pages, not endpoints. The ending
/
usually impliesindex.html
. So for endpoints, we want to make sure that accessing with or without trailing slashes is still possible and shouldn't be a big deal in practice.Goals
Note
Below lists 2 proposed changes, but they all achieve the same behaviour. Just different ways to model the API.
Proposed changes 1
New default
trailingSlash
config:trailingSlash.page
isalways
which matches the behaviour of the currentbuild.format: 'directory'
default. In practice, this should cause a breaking change of the file output. Same for endpoints too as it's stillignore
.build.format
is removed, and should be configured throughtrailingSlash.page
instead.build.format: 'directory'
, settrailingSlash: 'always'
build.format: 'file'
, settrailingSlash: 'never'
build.format: 'preserve'
, settrailingSlash: 'ignore'
(a little confusing but could polished up later)For easier migration, we can deprecate
build.format
for now, but auto-settrailingSlash.page
respectively based on the list above. IftrailingSlash.page
is already set but conflicts withbuild.format
, we could probably emit a warning.Proposed changes 2
build.format
only applies to pages, andtrailingSlash
only applies to endpoints.trailingSlash
option and only assumeignore
for endpoints. Usually endpoint trailing slash doesn't really matter so it might be good to have one less option to think about how it's applied?Personally I think "proposed changes 1" makes the API feel more consistent, but either way works well enough if documented.
Beta Was this translation helpful? Give feedback.
All reactions