opral / inlang-paraglide-js

Tree-shakable i18n library build on the inlang ecosystem.
https://inlang.com/m/gerre34r/library-inlang-paraglideJs
36 stars 0 forks source link

fetch() ignores exclude /api #191

Closed elron closed 4 weeks ago

elron commented 4 weeks ago

when I run this in my +page.svelte

const res = await fetch(`/api/reveal-pro/${confettiRef.id}`, { method: 'POST' });

it fetches from /he/api/... (with the language tag: /he or /en, when it should be fetch without language tag localhost:1234/api

image

even that I have /api in the exclude:

export const i18n = createI18n(runtime, {
  prefixDefaultLanguage: 'always',
  exclude: [
    '/api',
    '/test',
    '/a',
    '/sitemap.xml',
    '/example'
  ],
  textDirection: {
        en: "ltr",
        he: "rtl",
    },
});

How do I bypass that problem?

samuelstroschein commented 4 weeks ago

Does excluding the /api route fix the problem? see https://inlang.com/m/dxnzrydw/paraglide-sveltekit-i18n/advanced-usage#excluding-certain-routes

LorisSigrist commented 4 weeks ago

This is incredibly weird, fetch isn't supposed to be rewritten at all. exclude shouldn't be needed. I'll look into this

Are you using the normal fetch function or the one passed as an argument in +page.server.ts#load?

elron commented 4 weeks ago

I think I use the normal one, not svelte one. Because this runs on the browser.

elron commented 4 weeks ago

Does excluding the /api route fix the problem? see https://inlang.com/m/dxnzrydw/paraglide-sveltekit-i18n/advanced-usage#excluding-certain-routes

I can't disable it, otherwise I will literally have two API endpoints for two different languages. Exclude is needed

elron commented 4 weeks ago

I didn't notice before but I thought this is important to mention that this fetch function runs twice. Maybe this will beneficial: https://www.loom.com/share/1087f09d69544119a379c9832ceb8e94

LorisSigrist commented 4 weeks ago

Thanks for the video, that was really helpful

I think I know what's going on. fetch() itself isn't rewritten. Instead what's happening is that it's making requests to /api/*, which are then redirected to /[lang]/api/*. That's the second request you're seeing.

If you exclude /api it should work:

export const i18n = createI18n(runtime, {
  exclude: ["/api", /^\/api\/.*/]
})

Exclude in this case means "please don't redirect requests that start with /api" so you won't get multiple endpoints for different langauges.

elron commented 4 weeks ago

That fixed it!

Maybe add to the documentation?

LorisSigrist commented 4 weeks ago

I will!

elron commented 4 weeks ago

Thanks for the support