Skip to content

Commit

Permalink
<link> fixes re. modulepreload and fetch logic (#1614)
Browse files Browse the repository at this point in the history
* `link[rel="modulepreload"]` doesn't require `as="script"` which is tied to rel="preload" only
* extract file extension properly when examining `rel="prefetch"` to disregard URL parameters correctly

Co-authored-by: Eoghan Murray <eoghan@getthere.ie>
  • Loading branch information
billyvg and eoghanmurray authored Feb 14, 2025
1 parent dfb2991 commit 6f4e691
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/short-hounds-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"rrweb-snapshot": patch
"rrweb": patch
---

Change to ignore all link[rel="modulepreload"] instead of including only those with `as="script"`
14 changes: 9 additions & 5 deletions packages/rrweb-snapshot/src/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
type legacyAttributes,
} from '@rrweb/types';
import { type tagMap, type BuildCache } from './types';
import { isElement, Mirror, isNodeMetaEqual } from './utils';
import {
isElement,
Mirror,
isNodeMetaEqual,
extractFileExtension,
} from './utils';
import postcss from 'postcss';

const tagMap: tagMap = {
Expand Down Expand Up @@ -299,16 +304,15 @@ function buildNode(
continue;
} else if (
tagName === 'link' &&
(n.attributes.rel === 'preload' ||
n.attributes.rel === 'modulepreload') &&
n.attributes.as === 'script'
((n.attributes.rel === 'preload' && n.attributes.as === 'script') ||
n.attributes.rel === 'modulepreload')
) {
// ignore
} else if (
tagName === 'link' &&
n.attributes.rel === 'prefetch' &&
typeof n.attributes.href === 'string' &&
n.attributes.href.endsWith('.js')
extractFileExtension(n.attributes.href) === 'js'
) {
// ignore
} else if (
Expand Down
5 changes: 2 additions & 3 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,8 @@ function slimDOMExcluded(
(sn.tagName === 'script' ||
// (module)preload link
(sn.tagName === 'link' &&
(sn.attributes.rel === 'preload' ||
sn.attributes.rel === 'modulepreload') &&
sn.attributes.as === 'script') ||
((sn.attributes.rel === 'preload' && sn.attributes.as === 'script') ||
sn.attributes.rel === 'modulepreload')) ||
// prefetch link
(sn.tagName === 'link' &&
sn.attributes.rel === 'prefetch' &&
Expand Down

0 comments on commit 6f4e691

Please sign in to comment.