Discord activities Content Security Policy issue
Hi guys, currently, I'm going to use React + Convex to develop new Discord Activities.
However, I encountering a CSP issue when testing.
Discord suggests using URL Mapping to resolve it: https://discord.com/developers/docs/activities/development-guides#url-mapping
But I think Convex is using websocket, hope to get any suggestions or help. Thx!
Discord Developer Portal
Discord Developer Portal ā API Docs for Bots and Developers
Integrate your service with Discord ā whether it's a bot or a game or whatever your wildest imagination can come up with.
32 Replies
@ChrisLi did you try a mapping as suggested in https://discord.com/developers/docs/activities/development-guides#url-mapping I hope it would work the same way (but not sure)
yea, I see it uses prefix to solve the http issue, not sure how to do that with convex. I use vite to init my react project
What's different about Convex, did you try this?
Looks like it should work for WebSockets
this UI here
@ChrisLi I think I'm not understanding your question, are you asking what to put in that box?
yea, I don't know how to config that š„²
You don't know how to find that UI?
hmmm, I don't know which value should I put for url and target
I think you need to
- use a different URL in your React app: instead of https://sensible-skunk-264.convex.cloud, use something like https://your-activity/convex-api/ - but I'm not sure what goes in your-activity, where is the rest of your activity served from?
- In the Activity URL Mappings, map /convex-api to your domain, https://sensible-skunk-264.convex.cloud
If you don't know what URL your activity is served from then we may need to make some client changes: currenty the client expects an absolute url that includes the protocol, like
https://discord-activities-or-whatever/your-activity
@ChrisLi making a discord activity with Convex is cool, would love to hear how it goes!thanks! I'll do more research then.
I got some tries, and it still doesn't work.
Here is the config:
got the same csp issue as before
my frontend is pure vite+ react in my local, exposed by ngrok
communicate with convex directly inside my react
hmmm, also tried this way and still does not work:
Hi @ballingt , this issue still blocks me. Iām really appreciate if you have any insights! š
If you're doing that mapping I think the VITE_CONVEX_URL should be https://discord-activities-or-whatever/your-activity`, not the ngrok url
Where is the site hosted? How is the site exposed to the Discord UI?
i.e. if you just
fetch('/')
from the frontend, what URL is being fetched?
To avoid the CSP issue you need to hit a url like /convex-api.
But the COnvex client expects a qualified URL like https://some-discord-thing/convex-api
so you need to build that URL
If the URL isn't stable it might be necessary to change the Convex client to accept a relative pathIt works for me now! @ballingt
I used the fetch / to get the host url
Thank you very much!š
That's great! Looking forward to hearing about it, I've never written a Discord activity so I'd love to learn how.
What was the host URL? Don't have to reveal your real one, but roughly what does it look like? Just wondering in case future folks ask.
sure, the url is like this:
and for reference the config in the url mapper is like this:
the target url is the url we host the website
cc. @ballingt
hmmm, but I'm a little bit confused about the VITE_CONVEX_URL, what does it required? never touched it once start the Convex project. š§
cool, thanks
What does your code where you create a ConvexClient look like?
like this:
@ChrisLi that often that makes use of VITE_CONVEX_URL, which is automatically populated in the
.env.local
file.
So VITE_CONVEX_URL in .env.local is modified to point at your development deployment in dev, and your production deployment in prod.
But you need it to be something else, https//?????638.discordsays.com/convexapi
yea, but at first it ends with .convex.cloud, so, it can be modified?
so instead of using import.meta.env.VITE_CONVEX_URL there I would hardcode it to what you need it to be to work
ahhh ok yeah that part we need to fix
makes sense
this looks like the next problem https://github.com/get-convex/convex-js/blob/main/src/browser/sync/client.ts#L202
@ChrisLi You can hack this for now by providing a custom WebSocket constructor, but we'll change this to make it easier in the next client release
that's cool, thanks for letting me know!
skipConvexDeployomentUrlCheck
, and
- you can't use the automatically filled out VITE_CONVEX_URL value, because that won't be using the proxyahhh, ya, actually there is no more errors appears after I use the discord url to create the Convex Client!