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

can't access /keystatic route with Astro i18n integration and prefixDefaultLocale: true #1077

Open
marcob896 opened this issue Apr 17, 2024 · 5 comments

Comments

@marcob896
Copy link

Hi, I'm setting up keystatic in a localized project where the first segment of my url is the language locale.

my-domain.com/en/......
mydomain.com/es/.....

I'm not able to enter the keystatic panel because the localhost:4321/keystatic is matched by the src/pages/[lang] route.

I actually found the place where this configured and I added the /en prefix to make it work changing directly the file in my node_modules folder.

ui pattern
api pattern

injectRoute({
          // @ts-ignore
          entryPoint: '@keystatic/astro/internal/keystatic-astro-page.astro',
          entrypoint: '@keystatic/astro/internal/keystatic-astro-page.astro',
          pattern: '/en/keystatic/[...params]',
          prerender: false
        });
        injectRoute({
          // @ts-ignore
          entryPoint: '@keystatic/astro/internal/keystatic-api.js',
          entrypoint: '@keystatic/astro/internal/keystatic-api.js',
          pattern: '/en/api/keystatic/[...params]',
          prerender: false
        });

is there a way to set this up through normal config? I think it could be a useful feature

@simonswiss
Copy link
Collaborator

Hey!

I am not able to reproduce this issue — I can create a new Astro + Keystatic project and have a top-level [lang] dynamic segment, but the /keystatic route will still resolve to the Keystatic Admin UI.

By creating a new Keystatic + Astro project with the cli:

npm create @keystatic@latest

and selecting Astro...

I can then create a [lang] dynamic segment (or even a catch-all [...lang] segment), and the /keystatic route will still load the Keystatic Admin UI properly.

Do you have more details about the specifics of your setup?

@marcob896
Copy link
Author

marcob896 commented Apr 18, 2024

Hi, I think I spotted the root cause: I use the i18n Astro official integrations with prefixDefaultLocale = true, that's the problem.

This my astro config:

import { defineConfig } from 'astro/config';
import tailwind from '@astrojs/tailwind';
import react from '@astrojs/react';
import vercel from '@astrojs/vercel/serverless';
import markdoc from "@astrojs/markdoc";
import keystatic from '@keystatic/astro'

// https://astro.build/config
export default defineConfig({
  output: 'server',
  adapter: vercel(),
  prefetch: true,
  // trailingSlash: 'always',
  integrations: [tailwind(), react(), markdoc(), keystatic()],
  i18n: {
    defaultLocale: 'en',
    locales: ['en', 'it', 'es', 'fr'],
    routing: {
      prefixDefaultLocale: true
    },
    fallback: {
      it: 'en',
      es: 'en',
      fr: 'en'
    }
  }
});

If i set the prefixDefaultLocale: false keystatic will indeed work, but my website breaks.
Can you fix it?

@marcob896 marcob896 changed the title Can't access the admin page if I have a dynamic route at first url segment in Astro can't access /keystatic route with Astro i18n integrations and prefixDefaultLocale: true Apr 18, 2024
@marcob896 marcob896 changed the title can't access /keystatic route with Astro i18n integrations and prefixDefaultLocale: true can't access /keystatic route with Astro i18n integration and prefixDefaultLocale: true Apr 18, 2024
@simonswiss
Copy link
Collaborator

I've created an issue where I'll collate the numerous requests around i18n support in Keystatic — this should help justify carving some time to look into a first-party solution.

#1080

@stefanprobst
Copy link
Contributor

in astro 4.6, you can use the "manual" i18n routing strategy, and add your own middleware: https://docs.astro.build/en/guides/internationalization/#manual

@marcob896
Copy link
Author

marcob896 commented Apr 19, 2024

in astro 4.6, you can use the "manual" i18n routing strategy, and add your own middleware: https://docs.astro.build/en/guides/internationalization/#manual

That's great, so many thanks.
It seems to work with the following src/middleware.ts:

import { defineMiddleware } from 'astro:middleware';
import { redirectToDefaultLocale } from 'astro:i18n';



export const onRequest = defineMiddleware(async (ctx, next) => {
	if (ctx.url.pathname === '/') {
		return redirectToDefaultLocale(ctx, 302) as Response;
	}
	return next();
});

I can't make it work with the middleware function though, but everything seems to work as of now.

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

No branches or pull requests

3 participants