X Tutup
The Wayback Machine - https://web.archive.org/web/20201223160408/https://github.com/ionic-team/ionic-framework/pull/21249
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): Only set events in render function when running in browser #21249

Open
wants to merge 4 commits into
base: master
from

Conversation

@Yonom
Copy link

@Yonom Yonom commented May 8, 2020

Pull request checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Build (npm run build) was run locally and any changes were pushed
  • Lint (npm run lint) has passed locally and any fixes were made for failures

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

Issue Number: #19975
Rendering any Ionic component wrapped by createComponent on the server will cause an error:

ReferenceError: document is not defined
    at isCoveredByReact (\node_modules\@ionic\react\dist\index.js:267:50)

This makes Ionic incompatible with frameworks such as Gatsby or Next.js

What is the new behavior?

  • isCoveredByReact is only called if the code is running in the browser.
  • No events are passed to the web component if the renderer is not running in the browser.

Does this introduce a breaking change?

  • Yes
  • No

Other information

This fix mirrors a commit on the stencil-ds-plugins repository: ionic-team/stencil-ds-output-targets@b7b69ee

The ReactDOMServer passes the source code of an event handler as a string to the client if it does not recognize the element id. Therefore, an extra check for the onClick handler of routerLinkComponents was added. Without this check, this is the HTML sent to the client:

<ion-item onClick="(e) =&gt; {
    const { routerLink, routerDirection } = this.props;
    if (routerLink !== undefined) {
        e.preventDefault();
        this.context.navigate(routerLink, routerDirection);
    }
}">

The client side ReactDOM does not do this and can deal with onClick just fine. This was an interesting finding.

@Yonom
Copy link
Author

@Yonom Yonom commented May 8, 2020

In combination with #21132, Ionic can now be used in Gatsby / Next.js without the need for any workarounds.

@brandyscarney brandyscarney requested a review from elylucas May 26, 2020
@Yonom Yonom force-pushed the Yonom:patch-2 branch from 4913aa7 to b374583 Aug 15, 2020
@Yonom Yonom force-pushed the Yonom:patch-2 branch from 03077b7 to f476265 Sep 4, 2020
@Yonom Yonom force-pushed the Yonom:patch-2 branch from f476265 to a742db0 Sep 30, 2020
@Yonom Yonom force-pushed the Yonom:patch-2 branch from a742db0 to ed38414 Oct 18, 2020
@mount884
Copy link

@mount884 mount884 commented Oct 27, 2020

Hello team, is official nextjs support still a priority? Is it being worked on? Would love to be informed about it, regards

@Yonom Yonom force-pushed the Yonom:patch-2 branch from ed38414 to d13605b Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.
X Tutup