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 to work without errors in Nuxt Universal #723

Merged
merged 15 commits into from
Jul 31, 2020
Merged

Conversation

patarapolw
Copy link
Contributor

@patarapolw patarapolw commented May 25, 2020

As a solution to #722 I cloned and edited the repo.

  • In embed.ts,
    • Replace !. with ?. so it will not throw error ... of undefined.
    • Add destroy() to window.Remark42, which actually removes global listeners (on window and document)
    • Prevent duplication of Remark42 Iframe with
  const oldIframe = document.querySelector('iframe[title=Remark42]') as HTMLIFrameElement;
  if (oldIframe) {
    return oldIframe;
  }

Also, in production, instead of your init function https://github.com/umputun/remark42#comments, I moved it inside a function and add a typing for TypeScript. -- remark42.ts and it is implemented here -- PostFull.vue

In order to test in production, I deploy it to Google Compute Engine and Docker Hub with patarapolw/remark42, as can be seen at

@umputun
Copy link
Owner

umputun commented May 25, 2020

I'm not really sure what we supposed to see, but both links from the message above don't work. One returns "not found" (maybe you meant https://remark.polv.cc/web/ ?) and the second "Error: Server Error"

@patarapolw
Copy link
Contributor Author

@umputun Sorry, the website was down for some reasons (Google App Engine throws no log.) But it is back now.

Copy link
Collaborator

@Mavrin Mavrin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For SPA we could create api like:

const instance = window.REMARK42.createInstance({
  host: "host",
  site_id: "siteId",
  url: "url",
  max_shown_comments: 50,
  theme: "light",
  page_title: "test",
  node: DOMNode,
  locale: "en",
});
// instance.changeTheme();
// instance.destroy();

frontend/app/embed.ts Outdated Show resolved Hide resolved
frontend/app/embed.ts Outdated Show resolved Hide resolved
this.back!.setAttribute('data-animation', '');
this.node!.setAttribute('data-animation', '');
this.iframe!.focus();
this.back?.setAttribute('data-animation', '');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node should be always exist. Nullish operator should not be use for these cases. If we get null that mean that destroy doesn't work properly. It can create memory leak.

Copy link
Contributor Author

@patarapolw patarapolw Jun 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so it would probably become,

      if (!this.node) {
        this.node = document.createElement('div');
        ...
      }
      if (!this.back) {
        this.back = document.createElement('div');
        ...
      }
      this.iframe = iframe;
      ...
      setTimeout(() => {
        this.back!.setAttribute('data-animation', '');
        this.node!.setAttribute('data-animation', '');
        iframe.focus();
      }, 400);

@paskal
Copy link
Sponsor Collaborator

paskal commented Jun 14, 2020

Gentle ping for @patarapolw to change the code after the review.

@patarapolw
Copy link
Contributor Author

patarapolw commented Jun 14, 2020

Do you need some kind of commitiquette, like commitizen or commitlint?

Also, pre-commit Husky hooks should entail more IMO, rather than rely on pre-push.

~/projects/remark42(master*) » git commit                                                                                                    patarapolw@192
husky > pre-commit (node v12.16.3)
  ↓ Stashing changes... [skipped]
    → No partially staged files found...
  ✔ Running tasks...
husky > post-commit (node v12.16.3)
[master 6358acd2] fix onDestroy-related methods
 1 file changed, 16 insertions(+), 11 deletions(-)
------------------------------------------------------------------------------------------------------------------------------------------------------------
~/projects/remark42(master) » git push origin master                                                                                         patarapolw@192
husky > pre-push (node v12.16.3)

> remark-ui@0.1.0 check /Users/patarapolw/projects/remark42/frontend
> cross-env NODE_ENV=production npm run build && run-p check:*


> remark-ui@0.1.0 build /Users/patarapolw/projects/remark42/frontend
> webpack --mode production

NODE_ENV = production
REMARK_ENV = https://demo.remark42.com

@patarapolw patarapolw requested a review from Mavrin June 14, 2020 11:13
@akellbl4
Copy link
Collaborator

LGTM, but I would ask @Mavrin to revise.

@patarapolw
Copy link
Contributor Author

BTW, does not work in GatsbyJS, but OK in NextJS (and of course NuxtJS).

A reproducible repo is here -- https://github.com/patarapolw/jpdiary/tree/gatsby

@Mavrin
Copy link
Collaborator

Mavrin commented Jun 18, 2020

@patarapolw. Thank you for reproducible example. Did you read my comment?

For SPA we could create api like:

const instance = window.REMARK42.createInstance({
  host: "host",
  site_id: "siteId",
  url: "url",
  max_shown_comments: 50,
  theme: "light",
  page_title: "test",
  node: DOMNode,
  locale: "en",
});
// instance.changeTheme();
// instance.destroy();

I think if you impent this all should work as expected. You need to call this code when page is mount and unmount.

@patarapolw
Copy link
Contributor Author

patarapolw commented Jun 22, 2020

@patarapolw. Thank you for reproducible example. Did you read my comment?

For SPA we could create api like:

const instance = window.REMARK42.createInstance({
  host: "host",
  site_id: "siteId",
  url: "url",
  max_shown_comments: 50,
  theme: "light",
  page_title: "test",
  node: DOMNode,
  locale: "en",
});
// instance.changeTheme();
// instance.destroy();

I think if you impent this all should work as expected. You need to call this code when page is mount and unmount.

Ok, it seems that I have to frame the createInstance function. But, what is the best way to test it out without git commit and git push, that is relying on git hooks?

@Mavrin
Copy link
Collaborator

Mavrin commented Jun 23, 2020

You can run all test by npx run-p check lint.
I will look PR today.

frontend/app/embed.ts Outdated Show resolved Hide resolved
frontend/app/embed.ts Outdated Show resolved Hide resolved
frontend/app/embed.ts Outdated Show resolved Hide resolved
frontend/app/embed.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@Mavrin Mavrin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Could you please revert all replaced !. back?

@patarapolw
Copy link
Contributor Author

@Mavrin OK, but in theory, I am not sure when iframe.contentWindow will be null?

@Mavrin
Copy link
Collaborator

Mavrin commented Jun 25, 2020

@Mavrin OK, but in theory, I am not sure when iframe.contentWindow will be null?

If it is possible. We should add this check. Could you describe case when it is null?

@Mavrin
Copy link
Collaborator

Mavrin commented Jun 25, 2020

Lgtm.
Did you try run current solution on your example?
Thank you.

Mavrin
Mavrin previously approved these changes Jun 25, 2020
@akellbl4
Copy link
Collaborator

@patarapolw good job, would you be so kind to add documentation for this feature? And yes, as @Mavrin asked, it would be great to check working example.

@patarapolw
Copy link
Contributor Author

For the example, where I really used, see my website -- https://www.polv.cc.

I also made a much simpler example -- https://github.com/patarapolw/remark42-spa (will update in there.)

iframe.remove();
}

// TODO: These do not appear in Chrome DevTools
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have this unresolved problem. Not really a problem to me, but may affect backward compatibility...

Copy link
Collaborator

@akellbl4 akellbl4 Jul 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I see API appears in DevTools
Screenshot 2020-07-11 at 01 14 37

@umputun umputun merged commit 5d6729d into umputun:master Jul 31, 2020
@gvlasov
Copy link

gvlasov commented Aug 10, 2020

@patarapolw @umputun iframe reusing introduced in this PR breaks the possibility of having multiple comment sections on the same page. In this PR, iframe reuse is said to be for "Preventing duplication of Remark42 Iframe", but that definitely shouldn't be remark42's responsibility. It should be handled by client's component in frontend framework that uses remark42.

With current master, all that is needed to fix remark42 for multiple comment sections is changing

  const iframe =
    (document.querySelector('iframe[title=Remark42]') as HTMLIFrameElement) ||
    createFrame({ host: BASE_URL, query, __colors__: config.__colors__ });

to

  const iframe =
    createFrame({ host: BASE_URL, query, __colors__: config.__colors__ });

@gvlasov
Copy link

gvlasov commented Aug 10, 2020

A small correction: that's definintely not the only thing preventing multiple comment sections from working on the same page, but changing that is required if multiple comment sections are ever to be implemented (I'm probably going to be working on it soon and I will provide a PR)

@paskal paskal added this to the v1.7 milestone Jan 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants