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: ensure correct url is used for "subscribe to push window" #13

Merged
merged 4 commits into from
Nov 9, 2022
Merged

fix: ensure correct url is used for "subscribe to push window" #13

merged 4 commits into from
Nov 9, 2022

Conversation

3v0k4
Copy link
Contributor

@3v0k4 3v0k4 commented Nov 4, 2022

The "Enable Now" button is broken when using the embeddable JS snippet but not when using the React component.

That is because embeddable configures Vite/Rollup to alias axios to redaxios and redaxios does not implement getUri.

Repro steps

  • clone this repo
  • yarn build
  • add to example/index.html the snippet below
  • yarn start:example
  • open http://localhost:3000
  • you should see one bell at the top (React) and one below (embeddable)
  • click on the bell below (embeddable)
  • click on "Enable Now"
  • notice error in console Uncaught TypeError: Li.getUri is not a function
  • clear localstorage (so that you can see “Enable now” again)
  • click on the bell above (React)
  • click on “Enable Now”
  • notice no errors in console

Snippet

<div id="notifications-inbox" />

<script type="module">
  import { renderWidget } from '../packages/embeddable/dist/magicbell.esm.js';

  const targetElement = document.getElementById('notifications-inbox');
  const options = {
    apiKey: MY_API_KEY,
    userEmail: MY_USER_EMAIL,
    height: 500,
  };

  renderWidget(targetElement, options);
</script>

Solutions considered

  1. use axios instead of redaxios: this would work for sure but we prolly want to keep the deps footprint as small as possible
  2. replace axios.getUri in React with URL

This PR implements 2.

Difference between axios.getUri and URL

One thing I noticed that could be problematic is:

const uri = axios.getUri({
  url: "http://example.com",
  params: { p: undefined }
});
// http://example.com

uri2 = new URL('http://example.com')
uri2.searchParams.append("p", undefined)
uri2.toString()
// http://example.com/?p=undefined

There may be other encoding differences I'm not aware of.

Nota bene

I haven't run yarn changeset yet because I don't know how you want to bump this change.

Copy link
Member

@smeijer smeijer left a comment

Choose a reason for hiding this comment

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

Looking good. Thanks!

@smeijer smeijer changed the title Fix web notifications "Enable Now" button fix: ensure correct url is used for "subscribe to push window" Nov 9, 2022
@smeijer smeijer merged commit 1cb984c into magicbell:main Nov 9, 2022
@github-actions github-actions bot mentioned this pull request Nov 9, 2022
smeijer pushed a commit that referenced this pull request Nov 9, 2022
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @magicbell/[email protected]

### Patch Changes

- [#15](#15)
[`7767b69`](7767b69)
Thanks [@smeijer](https://github.com/smeijer)! - ensure that the
minified esm bundle is imported when importing from unpkg.

- Updated dependencies
\[[`1cb984c`](1cb984c)]:
    -   @magicbell/[email protected]

## @magicbell/[email protected]

### Patch Changes

- [#13](#13)
[`1cb984c`](1cb984c)
Thanks [@3v0k4](https://github.com/3v0k4)! - Fix embeddable web
notifications: since embeddable aliases axios (redaxios) and redaxios
does not implement `.getUri`, the code was failing for the embeddable
package (that uses redaxios) but not for the react package (that uses
axios).

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@3v0k4 3v0k4 deleted the fix-embeddable-web-notifications branch November 15, 2022 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants