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

🙋 how to keep html name #280

Closed
songlipeng2003 opened this issue Dec 15, 2017 · 18 comments
Closed

🙋 how to keep html name #280

songlipeng2003 opened this issue Dec 15, 2017 · 18 comments

Comments

@songlipeng2003
Copy link

<!DOCTYPE html>
<html>
<head>
	<title>home page</title>
  <script src="./scripts/index.js"></script>
</head>
<body>
	<img src="./images/logo.png">

	<a href="page.html">Link</a>
</body>
</html>

this page will compile to

<!DOCTYPE html>
<html>
<head>
	<title>home page</title>
  <script src="/dist/c4f6ff1a9fe4f3634b9272ba1ddaaf50.js"></script>
</head>
<body>
	<img src="/dist/98d8f0da23c4c8c6fe7083e3f62c1fdf.png">

	<a href="/dist/2f84526286378f5180fc0841e2f1a1d9.html">Link</a>
</body>
</html>

I want to keep html name, my expectation:

<!DOCTYPE html>
<html>
<head>
	<title>home page</title>
  <script src="/dist/c4f6ff1a9fe4f3634b9272ba1ddaaf50.js"></script>
</head>
<body>
	<img src="/dist/98d8f0da23c4c8c6fe7083e3f62c1fdf.png">

	<a href="/dist/page.html">Link</a>
</body>
</html>

Please tell me how to config ?

Software Version(s)
Parcel 1.2.0
Node 1.2.1
npm/Yarn
Operating System macos
@philipodev
Copy link
Contributor

this is somewhat related to #270

@DeMoorJasper
Copy link
Member

DeMoorJasper commented Dec 15, 2017

This shouldn't be a hard fix, related code:

parcel/src/Asset.js

Lines 146 to 166 in c008bb0

generateBundleName(isMainAsset) {
// Resolve the main file of the package.json
let main =
this.package && this.package.main
? path.resolve(path.dirname(this.package.pkgfile), this.package.main)
: null;
let ext = '.' + this.type;
// If this asset is main file of the package, use the package name
if (this.name === main) {
return this.package.name + ext;
}
// If this is the entry point of the root bundle, use the original filename
if (isMainAsset) {
return path.basename(this.name, path.extname(this.name)) + ext;
}
// Otherwise generate a unique name
return md5(this.name) + ext;
}

@DeMoorJasper DeMoorJasper changed the title how to keep html name 🙋 how to keep html name Dec 15, 2017
@ssuman
Copy link
Contributor

ssuman commented Dec 15, 2017

Should I include this in my change ?

@DeMoorJasper
Copy link
Member

@ssuman if you want, go for it :)

@ssuman
Copy link
Contributor

ssuman commented Dec 15, 2017

Sure will include this in my PR.

@Munter
Copy link

Munter commented Dec 25, 2017

In assetgraph-builder we also hash filenames, but exclude any files matching the following from renaming:

  • Any graph entry point (usually html)
  • Any asset linked to with an <a href>
  • Any asset linked to with a <meta http-equiv="refresh">
  • Serviceworkers (must keep consistent file name across builds)
  • humans.txt, robots.txt, '.htaccess`
  • Cache manifests, rss and atom feeds
  • favicon.ico

More rules might need to be added in the future of course, but these ones have covered us well for a couple of years

@eirikb
Copy link

eirikb commented Jan 17, 2018

Would it be possible to completely disable file name hashing with an option (to the cli)?

@DeMoorJasper
Copy link
Member

DeMoorJasper commented Jan 17, 2018

@eirikb this will be hard as it will not be prone to overwriting. For example index.js and ./src/somefolder/index.js will both be saved to index.js but they should be different bundles. However it might be possible to use a relative path and use that to write bundles into a directory tree similar to the source one. I'm not sure what the specific usecase for this is though.
Also for production this seems like a bad strategy, as the bundle names can and probably will in the future invalidate cdn and browser cache by changing the name of the bundle based on content.

@fregante
Copy link
Contributor

@DeMoorJasper the use case is having an actual site with human/SEO filenames, e.g.

example.com/
example.com/about.html
example.com/contacts.html

instead of

example.com/
example.com/98d8f0da23c4c8c6fe7083e3f62c1fdf.html <-- this looks so spammy
example.com/2f84526286378f5180fc0841e2f1a1d9.html

@Munter
Copy link

Munter commented Jan 22, 2018

Having pages that can be linked to from the outside because of predictable URL' s is a pretty big use case. It's the core functionality of the web

@DeMoorJasper
Copy link
Member

DeMoorJasper commented Jan 22, 2018

I kinda got aff-topic with my comment, talking about duplicates mainly focusing on javascript and such.
Anyways i understand the issue completely and agree this is a big issue. What do you guys think about something like this: (Pseudocode as explanation)

// HTML Asset getBundleName()
if (this.parent === null || this.parent.type === 'html') {
  // Only keep name if it's the main bundle or it's being required from html (for example through <a href=...
  // As this might possibly break if html is being used as a template inside JS, TS,...
  return path.join(this.relativePath, this.name);
}

This should only happen in htmlAssets (and maybe rawAsset - pictures and such) as far as i know

@Munter
Copy link

Munter commented Jan 22, 2018

@DeMoorJasper If you only look at the parent, you'll get a bunch of static assets that could easily be content addressable, but keep their non-hashed file name. When you are finding the edges in the graph are you not also recording the type of edge it is? This is highly valuable information for knowing what to rename under which circumstances.

The rule about keeping names on entry points and anything pointed to by an html anchor is good. But there are a few more quite important assets not to rename. See #280 (comment)

@opyate
Copy link

opyate commented Feb 28, 2018

Will this give me friendlier filenames to use with Angular 1.x directive templateUrl? Or, please point me to an example, as I can't find any.

@devongovett
Copy link
Member

Should be solved by #1025 which retains filenames and folder structure for linked files, and content hashes static assets. Please help test using the master branch - a release will hopefully come next week!

@HowardvanRooijen
Copy link

In assetgraph-builder we also hash filenames, but exclude any files matching the following from renaming:

  • Any graph entry point (usually html)
  • Any asset linked to with an <a href>
  • Any asset linked to with a <meta http-equiv="refresh">
  • Serviceworkers (must keep consistent file name across builds)
  • humans.txt, robots.txt, '.htaccess`
  • Cache manifests, rss and atom feeds
  • favicon.ico

More rules might need to be added in the future of course, but these ones have covered us well for a couple of years

Hi,

You mention rss and atom feeds - do you know what the naming convention should be used to ensure these files are ignored?

Currently I am producing a rss.xml and atom.xml and both of these are being hashed.

@Munter
Copy link

Munter commented Jun 3, 2020

I can only speak to what we did in assetgraph-builder. There we infer the asset type from the way it is marked up as a <link rel="alternate"> with a type attribute: https://github.com/assetgraph/assetgraph/blob/v6/lib/assets/Html.js#L477-L486

If you were to just anchorlink to the xml file it would just be inferred as an xml file with no special significance.

From looking at https://github.com/parcel-bundler/parcel/blob/master/packages/core/parcel-bundler/src/assets/HTMLAsset.js I can't see any similar special categorization of feeds and indeed https://github.com/parcel-bundler/parcel/tree/master/packages/core/parcel-bundler/src/assets also doesn't seem to have have any special handling. So in Parcel your feed is probably just treated equally to any other static asset with no special handling to keep urls stable

@HowardvanRooijen
Copy link

I currently have the following markup:

<link rel="alternate" type="application/atom+xml" href="/atom.xml" />
<link rel="alternate" type="application/rss+xml" href="/rss.xml" />

Which is being transformed into:

<link rel="alternate" type="application/atom+xml" href="/atom.c4df4b4f.xml">
<link rel="alternate" type="application/rss+xml" href="/rss.9a4ebc87.xml">

Which is less than ideal. I've read everything I could find, but I've not found a way to exclude these files. Any suggestions?

Many thanks!

@ghost
Copy link

ghost commented Sep 13, 2021

This shouldn't be a hard fix, related code:

parcel/src/Asset.js

Lines 146 to 166 in c008bb0

generateBundleName(isMainAsset) {
// Resolve the main file of the package.json
let main =
this.package && this.package.main
? path.resolve(path.dirname(this.package.pkgfile), this.package.main)
: null;
let ext = '.' + this.type;
// If this asset is main file of the package, use the package name
if (this.name === main) {
return this.package.name + ext;
}
// If this is the entry point of the root bundle, use the original filename
if (isMainAsset) {
return path.basename(this.name, path.extname(this.name)) + ext;
}
// Otherwise generate a unique name
return md5(this.name) + ext;
}

where am I suppose to add this piece of code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants