[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. |
Beta Was this translation helpful? Give feedback.
-
@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? |
Beta Was this translation helpful? Give feedback.
-
I think the |
Beta Was this translation helpful? Give feedback.
-
I think we should turn this into an RFC and include deprecating Astro.resolve as part of it. |
Beta Was this translation helpful? Give feedback.
-
In case anyone should pass by: |
Beta Was this translation helpful? Give feedback.
-
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.resolve
To author a relative path that is relative to the source, use
Astro.resolve
in Astro HTML.Pros:
Cons:
srcset
is used:Relative URLs in JavaScript Imports
To author a relative path that is relative to the source, use an
import
in 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
local
namespaced attribute.The
local
namespace would resolve relative paths in the attribute value while dropping the namespace from the attribute in the final output.Pros:
Cons:
local
namespace 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
import
with 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
local
schema.Pros:
Cons:
local
schema 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
import
with 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?
Beta Was this translation helpful? Give feedback.
All reactions