Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@ plugins: [
})
```

## Control where the generated tags are inserted in the result HTML

By default `PreloadWebpackPlugin` will insert the preload/prefetch tags in `<head></head>`.
You can control the inserting point by overriding the default option.

```javascript
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'src/index.html',
chunks: ['main']
}),
// I want to insert preload tags before this comment
new PreloadWebpackPlugin({
insertTagsBefore: '<!-- End of preload tags -->',
})
```

Resource Hints
---------------------

Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const defaultOptions = {
include: 'asyncChunks',
fileBlacklist: [/\.map/],
excludeHtmlNames: [],
insertTagsBefore: '</head>'
};

class PreloadPlugin {
Expand Down Expand Up @@ -281,9 +282,9 @@ class PreloadPlugin {
filesToInclude+= `<link rel="${options.rel}" href="${entry}">\n`;
}
});
if (htmlPluginData.html.indexOf('</head>') !== -1) {
// If a valid closing </head> is found, update it to include preload/prefetch tags
htmlPluginData.html = htmlPluginData.html.replace('</head>', filesToInclude + '</head>');
if (htmlPluginData.html.indexOf(this.options.insertTagsBefore) !== -1) {
// If a valid inserting point is found (default to </head>), update it to include preload/prefetch tags
htmlPluginData.html = htmlPluginData.html.replace(this.options.insertTagsBefore, filesToInclude + this.options.insertTagsBefore);
} else {
// Otherwise assume at least a <body> is present and update it to include a new <head>
htmlPluginData.html = htmlPluginData.html.replace('<body>', '<head>' + filesToInclude + '</head><body>');
Expand Down