Parcel v2 and absolute og:image #5271
Replies: 4 comments 4 replies
-
Have you taken a look at the public URL option? It allows you to force an absolute path. See the docs for target options. Also, if you don't want to have to rewrite the URLs from your existing site, make the site in your project root so that the paths are resolved as if they were relative. See the module resolution docs. |
Beta Was this translation helpful? Give feedback.
-
Hello @101arrowz ;-) Right now, I'm using Parcel with an npm script that calls this:
This way, Parcel takes all my HTML articles built by my static site generator. I saw the I also saw the The other problem that I tried to describe in my previous message is, I don't want absolute URLs everywhere, just where it's mandatory (like OG images or RSS feeds). |
Beta Was this translation helpful? Give feedback.
-
Reduced tests I did: Input relative URLInput: <!-- test-parcel/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta property="og:image" content="opengraph.jpg">
</head>
</html> Command:
Ouput: <!DOCTYPE html><html lang="en"><head><meta property="og:image" content="/opengraph.7bb51fde.jpg"></head></html> 👍 That's kind of the result I would expect:
🤔 Drawbacks: As I explained in my first comment, having Parcel handling those special meta tags is awesome but they will be useless if URLs are not absolute. Input relative URL with
|
Beta Was this translation helpful? Give feedback.
-
Looking again at my "propositions" (numbered this time):
As expected, this is not really something that can be done easily as some kind of option in core.
Being able to place a plugin before
I'm not sure if it's possible now, but if I could create a dependency with a custom |
Beta Was this translation helpful? Give feedback.
-
Hello 😄
Context
I'm using Parcel (v2) as an optimization step once my static site is generated. Therefore, I give Parcel a static site that already "works", with files that I could host as is. It's just that all those files are unminified, unbundled, images are unoptimized etc... I expect Parcel to take this static site and output a better version. One with optimized assets and where filenames are hashed for better cache.
Because the HTML source files I give to Parcel are 100% valid, my og:image URLs are already defined as full URLs with the target host, ex:
https://example.com/my-article/og-img.jpg
. This is expected by sites like Twitter. Relative URLs don't work.The problem
The HTML transformer looks for those kind of og:image URLs and it's one of the things I like about Parcel. These kind of details, especially for HTML, are handled. The problem is, because my og:image input URLs are "full" with the target host, Parcel can't "link" them to assets in the bundle graph and therefore can't rewrite them with the hashed filename of the optimized image.
Investigations
I tried and confirmed that giving Parcel HTML files with relative URLs for og:image works as an input. It recognizes the assets in the bundle graph and rewrites filenames with hashes properly.
I don't want to do this. An HTML file with relative URLs for og:image doesn't work once it's hosted online. I don't want to give Parcel a static site that doesn't work as is and I don't want Parcel to give me a static site that doesn't work as is. Technically, it outputs absolute URLs but without any target host so they're still "broken".
I had a look at the different HTML plugins (transformer, packager...). I also had a look at how dependencies and how URL replacements work.
Solution 1
I created a custom transformer for HTML files. I used it before the official HTML transformer in my config. This plugin knows about my target host and looks for og:image with full URLs. It computes a relative URL from the full URL, then calls
asset.addURLDependency(relativeUrl)
and replace the HTML node with the dependency id.This breaks because then the official HTML transformer sees a dependency id in the
content
attribute of the meta tag and cannot resolve it:I patched the HTML Transformer so it would ignore og:image meta tags and then the whole thing worked. When I say worked, I mean, I managed to make Parcel understand my full URLs with target host as un input, the ouput contained the filename with th hash but it was still an absolute URL without the target host.
Then I had the idea in my plugin to set the
content
attribute totargetHost + id
instead of just the idea. This worked. I finally had full URL as input and full URL (with hash) as output.Last problem was that I had to patch official HTML transformer.
Solution 2
I created another plugin but this time I place it after the official HTML transformer. Here's what it does:
content
attributecontent
attribute already containing an id withtargetHost + relativeId
where relativeId is the one from the dependency corresponding to the relative URL.THIS WORKS!! 🎉 This feels very hacky but I didn't have to patch the HTML transformer for this.
Proposition
Even if solution 2 works for me right now. I would like to discuss with the team how we can solve this in a better way.
Thanks
This was very long, sorry. I'm not sure if I make sense, let me know if I can clarify something. I know my context may be different from many Parcel users working with SPAs but I really think using Parcel to optimize a static site this way is a great goal.
Beta Was this translation helpful? Give feedback.
All reactions