Repository navigation
[Draft] Relative path handling in Astro #23
Replies: 5 comments 11 replies
In writing #28 I realized that CSS and JS are bundled based on the known import graph. If The promise of I mainly want to flag this because you phrased this as an absolute, that Astro needs to support late runtime resolution, but I don't think that this is true, at least for v1.0. Plenty of frameworks get around this limitation today without too much pain, as far as I can tell. This may change your proposal if this ends up to be the case. |
|
@jonathantneal I was confused by this discussion's title:
Is this a mistake in copy/pasting? I think this is unrelated to the markdown idea, no? |
|
I think the |
|
I think we should turn this into an RFC and include deprecating Astro.resolve as part of it. |
|
In case anyone should pass by: |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Relative path handling in Astro
Astro should make it easier to reference images and assets relative to the source file.
Examples:
Background
In Astro HTML, itβs not always easy to know where a relative path points to.
If a relative path is used inside of a component, then it might appear to be a reference to something relative to that component in the source. Instead, the path will be relative to the URL where the component was used in the final output.
Goals
Non Goals
/, because itβs much more complicated./is a relative link, while a path starting with/is an absolute path./to be used as a relative link, or as an absolute path, or as a project relative path.<a>,<link>,<script>, etc. and Astro users continue to face issues with these 1 2 3.Current Solutions:
Astro currently supports two solutions; one for 'late' resolution in the HTML block and one for 'early' resolution in the Frontmatter block.
Relative URLs in
Astro.resolveTo author a relative path that is relative to the source, use
Astro.resolvein Astro HTML.Pros:
Cons:
srcsetis used:Relative URLs in JavaScript Imports
To author a relative path that is relative to the source, use an
importin Astro Frontmatter.Pros:
Astro.resolve.Cons:
srcset, this still lacks cohesiveness with HTML.Proposed Solutions
Astro should continue to support two solutions; one for late resolution in the HTML block and one for early resolution in the Frontmatter block.
Relative URLs in Namespaced Attributes
To author a relative path that is relative to the source, use a
localnamespaced attribute.The
localnamespace would resolve relative paths in the attribute value while dropping the namespace from the attribute in the final output.Pros:
Cons:
localnamespace would need to be parsed to locate and transform relative paths.Relative URLs in Asserted JavaScript Imports
To author a relative path that is relative to the source, use an
importwith assertions in Astro Frontmatter.These type assertions would communicate how the file should be interpretted by JavaScript.
Pros:
Cons:
srcset, this still lacks cohesiveness with HTML.Alternative Solutions:
Relative URLs with a Custom Scheme
To author a relative path that is relative to the source, use a
localschema.Pros:
Cons:
localschema would need to be parsed in every attribute to locate and transform relative paths.Relative URLs with Query Parameters
To author a relative path that is relative to the source, use an
importwith query parameters in Astro Frontmatter.These query parameters communicate how the file should be loaded.
Pros:
Cons:
srcset, this still lacks cohesiveness with HTML.assert { type: 'json' }).Open Questions
Should all relative paths without custom syntax warn or throw?
No, Astro HTML should respect well known and well established conventions in HTML. If you intentionally use relative paths and are comfortable with this context, youβll be very glad to know that Astro wonβt make you change valid HTML.
Should Astro Support Multiple Solutions?
Yes, Astro needs to support late resolution and early resolution. For late resolution in Astro HTML, the solution should be cohesive with HTML. For early resolution in Astro Frontmatter, the solution should be cohesive with JavaScript.
How would import assertions work in Astro?
All reactions