dioxus icon indicating copy to clipboard operation
dioxus copied to clipboard

[Feature/Question] How to disable API URL randomization for a specific endpoint in fullstack?

Open wiseaidev opened this issue 1 year ago • 1 comments

Hiya (*・ω・)ノ,

I'm working on a new unique SASS template (our third (* ^ ω ^)!); Essentially a web app that lets users generate OG images in the browser locally using Gemini Nano. It involves converting HTML to images with html2canvas in JavaScript, which are then converted to Base64 and sent via fetch to an Axum endpoint, like so:

    fn save_preview_as_image() {
        client! {
            eval(r#"
                const element = document.getElementById('preview-section');
                if (element) {
                    html2canvas(element).then((canvas) => {
                        const base64Image = canvas.toDataURL('image/png');
                        const imageData = base64Image.replace(/^data:image\/png;base64,/, '');
                        const payload = `req[image_url]=${encodeURIComponent(imageData)}`;

                        // TODO: file an issue to disable api randomization
                        fetch('/api/upload_og6094941039135047799', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded'
                            },
                            body: payload
                        })
                        .then(response => {
                            dioxus.send(response);
                        })
                        .then(data => {
                        })
                        .catch(error => {
                        });
                    }).catch((error) => {
                    });
                } else {
                }
            "#);
        }

The issue is: with Dioxus fullstack, Dioxus/Axum is randomizing the API path URL at compile time (about every hour). To work around this, I've been calling the server function to reveal the URL in the Network tab and then hardcoding it into the JS code:

network tab

This feels...hacky. 😬 Is there a way to disable path randomization for a specific endpoint while keeping the rest of the endpoints intact?

Best! 🙏

wiseaidev avatar Dec 04 '24 11:12 wiseaidev

You can set the endpoint parameter in the server function macro like this:

#[server(
  endpoint = "upload_og",
)]

ealmloff avatar Dec 04 '24 13:12 ealmloff