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

feat: Revamp @formbricks/js package #2299

Conversation

ShubhamPalriwala
Copy link
Contributor

@ShubhamPalriwala ShubhamPalriwala commented Mar 20, 2024

What does this PR do?

  1. Renames current @formbricks/js to @formbricks/browser
  2. Builds a super lightweight @formbricks/js now that
    • fetches the latest JS SDK snippet from /api/js endpoint.
    • uses a Proxy to pass all formbricks.<commands> to window.formbricks.<command> to support future compatibility
    • Uses a Set to make sure multiple commands are not executed at the very same instance that might result in infinite recursive calls

Fixes #2104

How should this be tested?

Run the stack locally, give the demo app or any other js package using app a try, Monitor the console logs & network tab.

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read How we Code at Formbricks
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand bits
  • Ran pnpm build
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Formbricks Docs if changes were necessary

Copy link

vercel bot commented Mar 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
formbricks-cloud ⬜️ Ignored (Inspect) Visit Preview Mar 28, 2024 10:27am
formbricks-com ⬜️ Ignored (Inspect) Visit Preview Mar 28, 2024 10:27am

Copy link
Contributor

github-actions bot commented Mar 20, 2024

Thank you for following the naming conventions for pull request titles! 🙏

Copy link
Member

@mattinannt mattinannt left a comment

Choose a reason for hiding this comment

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

@ShubhamPalriwala thanks for this :-) Looks good!
I already talked with @pandeymangg about it: To reduce complexity I would love to remove the question side-loading from the js (now js-core) package. Can you please discuss with @pandeymangg how to make this possible in this PR? :-)

@@ -0,0 +1,61 @@
{
"name": "@formbricks/js-internal",
Copy link
Member

Choose a reason for hiding this comment

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

can we name it js-core and also use js-core instead of browser? :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

Or please something like js_core. Because building it as a iife does not support - in the module name

Copy link
Member

Choose a reason for hiding this comment

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

@jonas-hoebenreich 👍 What was the reason again why you prefer the iife format?

Copy link
Contributor

Choose a reason for hiding this comment

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

We initially had issues with umd leaking properties to the window object (which crashed our react shop). I just checked and it seems like this is no longer the case except the surveys package adding formbricks-surveys, formbricksSurveys & DOMPurify (below my code for reference).
We know that iife works for us, so before a switch to umd we would have to evaluate it again.

var initialWindowProperties = Object.keys(window);
var j = document.createElement('script');
j.async = true;
j.defer = true;
j.src = 'https://unpkg.com/@formbricks/surveys@1.99.0/dist/index.umd.js';
document.body.appendChild(j);
function logNewWindowProperties() {
    console.log("New Window Properties:");
    var currentWindowProperties = Object.keys(window);
    currentWindowProperties.forEach(function(prop) {
        if (!initialWindowProperties.includes(prop)) {
            console.log(prop);
        }
    });
}
j.onload = logNewWindowProperties;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hey @jonas-hoebenreich, I just tested the package with iife with the same name and it worked locally! Anything else that I can test with?

@@ -42,14 +42,8 @@
"@babel/core": "^7.24.0",
Copy link
Member

Choose a reason for hiding this comment

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

Can you please recheck what packages we need; I think we don't need the babel packages. I think we only need vite, terser & vite-plugn-dts

},
"author": "Formbricks <hola@formbricks.com>",
"devDependencies": {
"@babel/core": "^7.24.0",
Copy link
Member

Choose a reason for hiding this comment

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

Can you please recheck what packages we need; I think we don't need the babel packages & cross-env.

const isDevelopment = mode === "dev";
const formbricksSurveysScriptSrc = isDevelopment
? "http://localhost:3003/index.umd.js"
: `https://unpkg.com/@formbricks/surveys@~${surveysPackageJson.version}/dist/index.umd.js`;
Copy link
Member

Choose a reason for hiding this comment

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

Also the surveys package is currently still loaded from unpkg. This should also be loaded from the formbricks api endpoint :-)

@@ -0,0 +1,61 @@
{
"name": "@formbricks/js-internal",
Copy link
Contributor

Choose a reason for hiding this comment

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

Or please something like js_core. Because building it as a iife does not support - in the module name

@mattinannt mattinannt changed the title feat: @formbricks/browser & rewrite @formbricks/js package feat: Revamp @formbricks/js package Mar 21, 2024
packages/js/index.html Fixed Show fixed Hide fixed
…for-1993-rebuild-js-package-to-pull-script-from-formbricks-server
@jonas-hoebenreich
Copy link
Contributor

The api/js endpoint currently still has the following problem:
Nextjs automatically adds the header Vary: RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Url (https://github.com/vercel/next.js/blob/65d699e7d7b259af948990aa8052ffecc33812f5/packages/next/src/server/base-server.ts#L1717) which causes Proxies like Cloudflare or Cloundfront to exclude the endpoint from caching :/ So I think we need to find a way to remove this header because otherwise this will quickly overload the server (and add unnecessary egress fees when using Vercel).

@@ -2,12 +2,12 @@
<script type="text/javascript">
!(function () {
var t = document.createElement("script");
(t.type = "text/javascript"), (t.async = !0), (t.src = "./dist/index.umd.js");
(t.type = "text/javascript"), (t.async = !0), (t.src = "http://localhost:3000/api/packages/js-core");

Check warning

Code scanning / CodeQL

Inclusion of functionality from an untrusted source Medium

Script loaded using unencrypted connection.
@mattinannt mattinannt added this pull request to the merge queue Mar 28, 2024
@mattinannt mattinannt removed this pull request from the merge queue due to a manual request Mar 28, 2024
@mattinannt mattinannt added this pull request to the merge queue Mar 28, 2024
Merged via the queue into main with commit 0f95f1c Mar 28, 2024
12 of 14 checks passed
@mattinannt mattinannt deleted the shubham/for-1993-rebuild-js-package-to-pull-script-from-formbricks-server branch March 28, 2024 10:37
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.

[FEATURE] make widget not depend on internet access
3 participants