Skip to content

Commit

Permalink
Fixed .url() bug when mixing origins
Browse files Browse the repository at this point in the history
  • Loading branch information
Accudio committed Nov 5, 2022
1 parent 4844841 commit 27bb892
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
13 changes: 8 additions & 5 deletions dist/async-alpine.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ var AsyncAlpine = {
this._data[name].download = () => import(
/* @vite-ignore */
/* webpackIgnore: true */
url
this._parseUrl(url)
);
},
alias(path) {
Expand All @@ -132,10 +132,6 @@ var AsyncAlpine = {
let srcUrl = component.getAttribute(`${this._options.prefix}${this._options.inline}`);
if (!xData || !srcUrl)
return;
const absoluteReg = new RegExp("^(?:[a-z+]+:)?//", "i");
if (!absoluteReg.test(srcUrl)) {
srcUrl = new URL(srcUrl, document.baseURI).href;
}
const name = this._parseName(xData);
this.url(name, srcUrl);
},
Expand Down Expand Up @@ -248,5 +244,12 @@ var AsyncAlpine = {
const parsedName = (attribute || "").split(/[({]/g)[0];
const ourName = parsedName || `${internalNamePrefix}${this._index}`;
return ourName;
},
_parseUrl(url) {
const absoluteReg = new RegExp("^(?:[a-z+]+:)?//", "i");
if (!absoluteReg.test(url)) {
return new URL(url, document.baseURI).href;
}
return url;
}
};
13 changes: 8 additions & 5 deletions dist/async-alpine.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ var AsyncAlpine = {
this._data[name].download = () => import(
/* @vite-ignore */
/* webpackIgnore: true */
url
this._parseUrl(url)
);
},
alias(path) {
Expand All @@ -119,10 +119,6 @@ var AsyncAlpine = {
let srcUrl = component.getAttribute(`${this._options.prefix}${this._options.inline}`);
if (!xData || !srcUrl)
return;
const absoluteReg = new RegExp("^(?:[a-z+]+:)?//", "i");
if (!absoluteReg.test(srcUrl)) {
srcUrl = new URL(srcUrl, document.baseURI).href;
}
const name = this._parseName(xData);
this.url(name, srcUrl);
},
Expand Down Expand Up @@ -235,6 +231,13 @@ var AsyncAlpine = {
const parsedName = (attribute || "").split(/[({]/g)[0];
const ourName = parsedName || `${internalNamePrefix}${this._index}`;
return ourName;
},
_parseUrl(url) {
const absoluteReg = new RegExp("^(?:[a-z+]+:)?//", "i");
if (!absoluteReg.test(url)) {
return new URL(url, document.baseURI).href;
}
return url;
}
};
export {
Expand Down
2 changes: 1 addition & 1 deletion dist/async-alpine.script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "async-alpine",
"version": "0.5.3",
"version": "0.5.4",
"description": "Load Alpine Componenets asynchronously. Allows for code splitting and lazy loading components!",
"author": "Alistair Shepherd <[email protected]>",
"license": "Apache-2.0",
Expand Down
20 changes: 12 additions & 8 deletions src/core/async-alpine.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const AsyncAlpine = {
this._data[name].download = () => import(
/* @vite-ignore */
/* webpackIgnore: true */
url
this._parseUrl(url)
);
},

Expand Down Expand Up @@ -98,13 +98,6 @@ const AsyncAlpine = {
let srcUrl = component.getAttribute(`${this._options.prefix}${this._options.inline}`);
if (!xData || !srcUrl) return;

// if the URL is relative then convert it to absolute based on the document baseURI
// this is needed for when async alpine is loaded from a different origin than the page and component
const absoluteReg = new RegExp('^(?:[a-z+]+:)?//', 'i');
if (!absoluteReg.test(srcUrl)) {
srcUrl = new URL(srcUrl, document.baseURI).href;
}

const name = this._parseName(xData);
this.url(name, srcUrl);
},
Expand Down Expand Up @@ -308,6 +301,17 @@ const AsyncAlpine = {
const ourName = parsedName || `${internalNamePrefix}${this._index}`;
return ourName;
},

_parseUrl(url) {
// if the URL is relative then convert it to absolute based on the document baseURI
// this is needed for when async alpine is loaded from a different origin than the page and component
const absoluteReg = new RegExp('^(?:[a-z+]+:)?//', 'i');
if (!absoluteReg.test(url)) {
return new URL(url, document.baseURI).href;
}

return url;
},
};

export { AsyncAlpine };

0 comments on commit 27bb892

Please sign in to comment.