Skip to content

Commit 5f1c3ba

Browse files
authored
Merge pull request #263 from Bijikyu/q/modify-injectcss-to-prioritize-data-qorecss-script
Fix injection search order
2 parents 137ba0a + 19fad63 commit 5f1c3ba

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,16 @@ if (typeof window === 'undefined' && typeof module !== 'undefined' && module.exp
155155
function injectCss(){ // handles runtime stylesheet loading logic
156156
console.log(`injectCss is running with ${document.currentScript && document.currentScript.src}`); // logs entry and script src
157157
try {
158-
let scriptEl = document.currentScript; // uses current script element when available
159-
if(!scriptEl){ // falls back to iterating all script tags when currentScript missing
158+
let scriptEl = document.querySelector('script[data-qorecss]'); // prioritizes explicit attribute for accurate base path
159+
if(!scriptEl){ scriptEl = document.currentScript; } // falls back to currentScript when attribute missing
160+
if(!scriptEl){ // iterates when neither attribute nor currentScript found
160161
const scripts = Array.from(document.getElementsByTagName('script')); // gathers all script elements for manual search
161162
scriptEl = scripts.find(s=>{ // searches for matching script by pathname
162163
try{ // protects against invalid URLs in script tags
163164
return new URL(s.src, document.baseURI).pathname.toLowerCase().endsWith('index.js'); // case-insensitive compare on parsed pathname
164165
}catch{return false;} // ignores scripts with bad URLs
165166
});
166167
}
167-
if(!scriptEl){ scriptEl = document.querySelector('script[data-qorecss]'); } // selects only script elements for reliable base path
168168
const scriptSrc = scriptEl && scriptEl.src ? scriptEl.src : ''; // avoids errors when element or src missing
169169
let basePath = ''; // default empty base path
170170
if (scriptSrc) {

test/index.browser.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ describe('browser injection', {concurrency:false}, () => {
155155
assert.ok(link.href.startsWith('https://cdn.example.com/data/')); // ensures detection via data attribute
156156
});
157157

158+
it('prioritizes data-qorecss over other index.js scripts', () => {
159+
const other = document.createElement('script'); // unrelated index.js for fallback search
160+
other.src = 'https://cdn.other.com/ignore/index.js'; // path not used when data attribute exists
161+
document.body.appendChild(other); // adds non-attribute script first
162+
const target = document.createElement('script'); // script marked for css base path
163+
target.src = 'https://cdn.target.com/assets/index.js'; // correct base path for injection
164+
target.setAttribute('data-qorecss', ''); // attribute marks this script
165+
document.body.appendChild(target); // adds attribute script after other
166+
delete require.cache[require.resolve('../index.js')]; // ensures fresh module load
167+
require('../index.js'); // executes injection
168+
const link = document.querySelector('link'); // link inserted by injectCss
169+
assert.ok(link.href.startsWith('https://cdn.target.com/assets/')); // verifies attribute script used
170+
});
171+
158172
it('defaults to document.baseURI when script not found', () => {
159173
require('../index.js'); // loads module with no identifiable script
160174
const link = document.querySelector('link'); // retrieves injected link

0 commit comments

Comments
 (0)