When using AlpineJS in IE11, utilising the IE11 version of the Alpine script via CDN or as part of a bundle, it won't register the extracted component logic or work like it does in other browsers.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div x-data="teamProfileModal()">
<button @click="openModal()">Open</button>
<div x-show="isOpen()">
<h1>This is content</h1>
<p>This is content description.</p>
<button @click="closeModal()">Close</button>
</div>
</div>
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine-ie11.js" defer></script>
<script>
function hideBodyOverflow() {
document.body.style.overflow = 'hidden'
}
function showBodyOverflow() {
document.body.style.overflow = 'visible'
}
function teamProfileModal() {
return {
open: false,
openModal() { this.open = true, hideBodyOverflow() },
closeModal() { this.open = false, showBodyOverflow() },
isOpen() { return this.open === true },
}
}
</script>
</body>
</html>
When the page loads, we're expecting the header, content and close button to be hidden and only the 'Open' button should be visible on the page.
All elements are shown on the page due to Alpine not being able to work.
I could not provide a CodePen due to IE11 not being supported by CodePen.
This is how the page look when loading in IE11:

These are the console errors that IE11 is spitting out:

This works in other browsers including Edge, just not IE11.
It's worth noting that this was previously working on the 25th Feb 2020, but I was testing via Browserstack and I'm not sure if Browserstack does something to make Alpine work, or if this has always been broken. I've only recently started testing via a VM.
The latest build breaks on IE11
You can pin to 2.0.2 and it should work
The latest build breaks on IE11
You can pin to 2.0.2 and it should work
That should explain it, thank you! I'll give it a go! 馃榾
See this issue https://github.com/alpinejs/alpine/issues/241 PR is open to fix this
Let me know if dropping down to 2.0.x fixes your issue or if there's an outstanding IE11 issue
See this issue #241 PR is open to fix this
Let me know if dropping down to 2.0.x fixes your issue or if there's an outstanding IE11 issue
Hi @HugoDF thanks for your detailed responses, it's much appreciated.
I have tried dropping down the versions and the issue still exists.
I've had another look and I think the issue is with your code not Alpine.js see the following (as per your example):
function聽teamProfileModal()聽{
return聽{
聽聽 open:聽false,
聽聽聽聽openModal()聽{聽this.open聽=聽true,聽hideBodyOverflow()聽},
聽聽聽聽closeModal()聽{聽this.open聽=聽false,聽showBodyOverflow()聽},
聽聽聽 isOpen()聽{聽return聽this.open聽===聽true聽},
聽聽聽}
}
IE11 doesn't support object shorthand properties/functions see https://caniuse.com/#search=Object%20shorthand
Solution is to use long hand notation for openModal, closeModal and isOpen
function聽teamProfileModal()聽{
return聽{
聽聽 open:聽false,
openModal: function()聽{聽this.open聽=聽true,聽hideBodyOverflow()聽},
closeModal: function()聽{聽this.open聽=聽false,聽showBodyOverflow()聽},
isOpen: function()聽{聽return聽this.open聽===聽true聽},
}
}
No way! I swear I tried this, obviously I've implemented it wrong. Thank you so much Hugo! Works as expected.
Most helpful comment
I've had another look and I think the issue is with your code not Alpine.js see the following (as per your example):
IE11 doesn't support object shorthand properties/functions see https://caniuse.com/#search=Object%20shorthand
Solution is to use long hand notation for
openModal,closeModalandisOpen