vercel / analytics

Privacy-friendly, real-time traffic insights
https://vercel.com/analytics
Mozilla Public License 2.0
416 stars 24 forks source link

Can't use server track in Remix #111

Closed JClackett closed 9 months ago

JClackett commented 10 months ago

It seems the track function uses the "use-server" package and that fails to detect when running in a remix action apparently.

I've got a analytics.server.ts file which exports the track function from this package, ive added a "use server" line at the top but I still get:

Error: This module cannot be imported from a Client Component module. It should only be used from a Server Component.

Any ideas what I could be doing wrong?

p.s also had to add @vercel/analytics to remix's serverDependenciesToBundle config

tobiaslins commented 9 months ago

Hey! Can you please provide us a minimal reproduction? Would love to dig into this

JClackett commented 9 months ago

Here it is, it's just a stripped back remix on vercel template that imports the analytics function from a .server file

https://github.com/JClackett/remix-vercel-analytics

the dev server won't even run because the "server-only" package throws an exception

JClackett commented 9 months ago

In the meantime I've just patched there server file to not import "use-server"

diff --git a/dist/server/index.cjs b/dist/server/index.cjs
index 3dd9fdb6d0c56f4709a2f168f00264c3b0019518..01715438f3336772dd7baa4e5c4ca975ad8d73a6 100644
--- a/dist/server/index.cjs
+++ b/dist/server/index.cjs
@@ -23,7 +23,7 @@ __export(server_exports, {
   track: () => track
 });
 module.exports = __toCommonJS(server_exports);
-var import_server_only = require("server-only");
+// var import_server_only = require("server-only");

 // src/utils.ts
 function isBrowser() {
diff --git a/dist/server/index.js b/dist/server/index.js
index 9fed2b1b428b3608e881eb99cd1975f42f6a7f16..320d50eddd9acb866594cab6b25d08321675e6c2 100644
--- a/dist/server/index.js
+++ b/dist/server/index.js
@@ -1,5 +1,5 @@
 // src/server/index.ts
-import "server-only";
+// import "server-only";

 // src/utils.ts
 function isBrowser() {
tobiaslins commented 9 months ago

Thanks! We're trying to figure out how remix works for bundling. seems like server-only package mostly just useful for Next.js

tobiaslins commented 9 months ago

We'll push the new release out today (1.1.1) which fixes the issue!