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: migrate to cloudflare pages and vite (Work in progress) #81

Closed
wants to merge 14 commits into from

Conversation

BhanuNexus
Copy link
Contributor

@BhanuNexus BhanuNexus commented Jun 20, 2024

Migrate to Remix Vite and Cloudflare Pages

Status: Work in progress
Issue: #55

@benvinegar
Copy link
Owner

Wow, awesome – I took a stab at this for a month+ ago and didn't end up finishing.

Let me know how I can help!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trying to understand on how to update this tests

@BhanuNexus
Copy link
Contributor Author

Hi @benvinegar, Whenever you get sometime, could you please checkout this branch and test? I don't have data for my cloudflare account, hence not able to test the dashboard completely.

Apart from collect.test.ts, remaining looks good in my tests.
FYI - I did npm package updates as well.

@benvinegar
Copy link
Owner

@BhanuNexus I think I addressed the tests.

But I don't know how to actually run this. If I try npm run dev it will load the homepage, but /collect 404s and trying to load the dashboard URL crashes the server.

It looks like this is converted to Cloudflare Pages (e.g. the functions dir), so I also tried wrangler pages dev but then I get the following compilation errors:

❯ node_modules/wrangler/bin/wrangler.js pages dev

 ⛅️ wrangler 3.61.0
-------------------

✘ [ERROR] 1 error(s) and 0 warning(s) when compiling Worker.



✘ [ERROR] Could not resolve "../build/server"

    [[page]].ts:3:23:
      3 │ import * as build from "../build/server";
        ╵                        ~~~~~~~~~~~~~~~~~


✘ [ERROR] Build failed with 1 error:

  [[page]].ts:3:23: ERROR: Could not resolve "../build/server"

Maybe I'm missing some steps/instructions?

@BhanuNexus
Copy link
Contributor Author

BhanuNexus commented Jun 21, 2024

@benvinegar could you please try below?

# added prepreview script now
npm run preview

@BhanuNexus
Copy link
Contributor Author

I think I addressed the tests.

Thank you :)

@benvinegar
Copy link
Owner

@benvinegar could you please try below?

Yep, this gets the server running -- thank you -- but looks like it's producing a bad query because it can't extract the timezone correctly. Poking at this.

@benvinegar
Copy link
Owner

benvinegar commented Jun 21, 2024

Ok, I think it works: the only remaining issue is figuring out how to inject VERSION into the context.

This doesn't seem to ever get a value:

context.cloudflare.env.CF_PAGES_COMMIT_SHA

Any ideas?

@benvinegar
Copy link
Owner

Here's your PR branch running locally, serving my production dataset:

counterscale-cf-pages.mov

@benvinegar
Copy link
Owner

Trying to deploy this and I basically just get 500 Internal Error with no visibility into what's going on / no actual HTML returned or anything.

@BhanuNexus
Copy link
Contributor Author

BhanuNexus commented Jun 22, 2024

Thank you very much for your changes, sorry for trouble with wrong parameter for timezone.

I am assuming there are no secrets set, can you try below?

# for production
npx wrangler pages secret put CF_ACCOUNT_ID --env production
npx wrangler pages secret put CF_BEARER_TOKEN --env production

# for preview
npx wrangler pages secret put CF_ACCOUNT_ID --env preview
npx wrangler pages secret put CF_BEARER_TOKEN --env preview

I'm able to run it here https://counterscaleapp.pages.dev

@BhanuNexus
Copy link
Contributor Author

This doesn't seem to ever get a value:

context.cloudflare.env.CF_PAGES_COMMIT_SHA

Any ideas?

This value is available with CF build environment (similar to github actions variables)
I can see value is available once deployed to pages, sample https://counterscaleapp.pages.dev/

@benvinegar
Copy link
Owner

I am assuming there are no secrets set, can you try below?

Sorry will try to poke at this tonight. I do wish there was better reporting coming out of pages or a way to capture this without blowing up.

@benvinegar
Copy link
Owner

benvinegar commented Jun 26, 2024

Yep, that did it 🎉

# for preview
npx wrangler pages secret put CF_ACCOUNT_ID --env preview
npx wrangler pages secret put CF_BEARER_TOKEN --env preview

^Note I didn't need this I think because (I think) it reads .dev.vars which already had my local secrets.


Okay, so my thinking is: this is basically a major version. It's basically a re-architecture of what I'm used to, and I'm not super comfortable just making this main.

So here's my suggestion:

  1. I actually tag a 1.0.0 release of Counterscale of HEAD on main
  2. I start a v2 branch and merge this PR into that branch
  3. Going forward, new feature development happens on the v2 branch
    1. I'll change counterscale.dev to autodeploy from the v2 branch
  4. After some time when I feel it's stable, there are no concerns, etc., I'll merge the v2 branch into main and tag a 2.0.0 release

@BhanuNexus Thoughts on this plan?

@BhanuNexus
Copy link
Contributor Author

Note I didn't need this I think because (I think) it reads .dev.vars which already had my local secrets.

yeah, this is an issue and this also makes me think of using Cloudflare Pages settings instead of considering wrangler.toml.
If we remove pages_build_output_dir = "build/client" from wrangler.toml then variables can be set from cloudflare UI, that way it might be easy for people who fork this repo and use.

@BhanuNexus
Copy link
Contributor Author

BhanuNexus commented Jun 26, 2024

Yep, that did it 🎉

# for preview
npx wrangler pages secret put CF_ACCOUNT_ID --env preview
npx wrangler pages secret put CF_BEARER_TOKEN --env preview

^Note I didn't need this I think because (I think) it reads .dev.vars which already had my local secrets.

Okay, so my thinking is: this is basically a major version. It's basically a re-architecture of what I'm used to, and I'm not super comfortable just making this main.

So here's my suggestion:

  1. I actually tag a 1.0.0 release of Counterscale of HEAD on main

  2. I start a v2 branch and merge this PR into that branch

  3. Going forward, new feature development happens on the v2 branch

    1. I'll change counterscale.dev to autodeploy from the v2 branch
  4. After some time when I feel it's stable, there are no concerns, etc., I'll merge the v2 branch into main and tag a 2.0.0 release

@BhanuNexus Thoughts on this plan?

Totally agreed.
Meanwhile, I can update documentation and need to check posibility of moving cloudflare function /collect to remix, this will help to reduce dependency on CF and devlopment/debugging will be easy in local.

@benvinegar
Copy link
Owner

Just following up – I've been on vacation, but back now and plan to make these branch changes soon.

Meanwhile, I can update documentation and need to check posibility of moving cloudflare function /collect to remix, this will help to reduce dependency on CF and devlopment/debugging will be easy in local.

👍

@@ -130,7 +130,7 @@ interface DataPoint {
// More here: https://developers.cloudflare.com/analytics/analytics-engine/get-started/#limits

export function writeDataPoint(
analyticsEngine: CFAnalyticsEngine,
analyticsEngine: AnalyticsEngineDataset,
Copy link
Owner

Choose a reason for hiding this comment

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

I think generally speaking, this change isn't really related to migrating to vite, and more of a separate personal choice that's been inserted (same with renaming Environment to Env).

I'll keep it (it's more clear I suppose) but generally it's best to not to couple these changes.

Copy link
Contributor Author

@BhanuNexus BhanuNexus Jul 13, 2024

Choose a reason for hiding this comment

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

Agreed, this AnalyticsEngineDataset type is directly from @cloudflare/workers-types
So, syntaxes and change tracking are aligned with CF.

Regarding Env, I personally prefer Environment whatever you have set but cloudflare auto generates types from wrangler.toml to worker-configuration.d.ts by using wrangler types, that is the reason I have removed Environment and reused Env from worker-configuration.d.ts

"dev": "remix vite:dev",
"build": "remix vite:build",
"prepreview": "remix vite:build",
"preview": "wrangler pages dev ./build/client",
Copy link
Owner

Choose a reason for hiding this comment

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

When should I use npm run preview and when should I use npm run dev? Are the remix commands deprecated now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wrangler pages dev ./build/client command simulate the CF environment locally like functions
Where as remix vite:build just works with remix, usually this command alone will have 404 for /collect endpoint of CF functions.

If we manage to migrate /collect endpoint to remix, then preview wouldn't be needed anymore. I believe this is doable with minor changes, I will try to get this done in couple of days.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @benvinegar, I have previously added this endpoint to test movement of /collect from CF function to remix.

I have tested this with mine https://cs.usevega.com and seems to be working fine, do you want to have this change included with this PR or may be in later iteration?

Copy link
Owner

Choose a reason for hiding this comment

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

@BhanuNexus I started the v2 branch for this ongoing migration (here). I'd make a new PR targeting that.

I'm going to close this PR in the meantime.

@benvinegar
Copy link
Owner

Closing because this PR has become the v2 branch.

@benvinegar benvinegar closed this Jul 14, 2024
@benvinegar
Copy link
Owner

@BhanuNexus All your changes are now on main (version is v2.0.0-beta.2).

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