You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on a component library and we are using a non-Stencil custom element <focus-trap>. When we use <focus-trap> in our project prerendering our code with the renderToString method we get from the dist-hydrate-script build target. We get this error.
window.customElements.define("focus-trap", FocusTrap);
^
TypeError: Cannot read property 'define' of null
Expected behavior:
Give the info in the existing prerendering guide I wouldn't expect this to work. BUT given that Stencil promotes Web Components so heavily I think it could be reasonable expected the Stencil shouldn't choke on this because people might pull in other web component based code into their Stencil projects.
Steps to reproduce:
In a new Stencil project install focus trap npm i @a11y/focus-trap and make a component like:
import{Component,h}from"@stencil/core";import"@a11y/focus-trap";
@Component({tag: "my-component",styleUrl: "my-component.css",shadow: true})exportclassMyComponent{render(){return(<focus-trap><div>Hello, World! I'm in a focus trap!</div></focus-trap>);}}
Then add { type: "dist-hydrate-script" } to your outputTargets in stencil.config.js and run stencil build.
This error occurs as soon as the code from focus-trap is evaluated. I tried something like
if(Build.isBrowser){import("@a11y/focus-trap").then(()=>{// set a flag to know if I should wrap in <focus-trap> (browser) for a <div> (prerender)});}
But this doesn't work. The code still gets evaluated right away and the error gets thrown.
This could be solved in focus trap by wrapping window.customElements.define in an if statement works but this only solve this issue for focus trap not all custom elements.
Hey there, thank you for the patience getting back to you. The new team is getting started and we're working through the backlog now.
We do not support Stencil versions less than v2, and you seem to be using an older version of Stencil. If you can upgrade to the latest version and let me know if this issue still exists, I would appreciate it.
Thanks for the issue! This issue is being closed due to inactivity. If this is still an issue with the latest version of Stencil, please create a new issue and ensure the template is fully filled out.
Stencil version:
I'm submitting a:
[x] bug report
[ ] feature request
[ ] support request => Please do not submit support requests here, use one of these channels: https://stencil-worldwide.herokuapp.com/ or https://forum.ionicframework.com/
Current behavior:
I'm working on a component library and we are using a non-Stencil custom element
<focus-trap>
. When we use<focus-trap>
in our project prerendering our code with therenderToString
method we get from thedist-hydrate-script
build target. We get this error.Expected behavior:
Give the info in the existing prerendering guide I wouldn't expect this to work. BUT given that Stencil promotes Web Components so heavily I think it could be reasonable expected the Stencil shouldn't choke on this because people might pull in other web component based code into their Stencil projects.
Steps to reproduce:
In a new Stencil project install focus trap
npm i @a11y/focus-trap
and make a component like:Then add
{ type: "dist-hydrate-script" }
to youroutputTargets
instencil.config.js
and runstencil build
.next call
renderToString
:Related code:
It looks like https://github.com/ionic-team/stencil/blob/90f72634c579c5e4f16a88b0ab672d7ac8c5d7b2/src/hydrate/runner/window-initialize.ts#L39-L41 is what causes this by setting
window.customElements
back tonull
it make it unavailable. I tried commenting this out and everything seems to work fine but I don't know what other negative impacts removing this might have.Other information:
This comes from https://github.com/andreasbm/focus-trap/blob/master/src/lib/focus-trap.ts#L208 which assumes it is being loaded on the client. While the prerendering guide talks about using
Build.isBrowser
variable to avoid cases like this there are a few reasons why this is difficult in this case.focus-trap
is evaluated. I tried something likewindow.customElements.define
in an if statement works but this only solve this issue for focus trap not all custom elements.The text was updated successfully, but these errors were encountered: