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

Fixes draft preview links when i18n is enabled in Next.js config #853

Merged
merged 2 commits into from
Apr 29, 2022

Conversation

apmatthews
Copy link
Member

@apmatthews apmatthews commented Apr 27, 2022

Description

Next.js includes support for internationalized routing through the i18n config. The i18n config affects redirects in such a way that our existing preview regex doesn't match on any root path (i.e. /, /en, /fr, etc.). This PR adds an extra redirect rule to handle the special cases that i18n introduces.

Preview links that target a root path are typically draft posts that have not been published. These posts don't have a post slug yet, so they are identified only by query params on the site's root path.

Testing

Using the following config...

const { withFaust } = require('@faustjs/next');

/**
 * @type {import('next').NextConfig}
 **/
module.exports = withFaust({
  i18n: {
    locales: ['en', 'fr'],
    defaultLocale: 'en',
  },
});

you should see these results:

Before

Status Preview URL Result
Draft /?preview=true&p=1&typeName=Post /?preview=true&p=1&typeName=Post 🔴
Draft /en?preview=true&p=1&typeName=Post /?preview=true&p=1&typeName=Post 🔴
Draft /fr?preview=true&p=1&typeName=Post /fr?preview=true&p=1&typeName=Post 🔴
Published /posts/hello-world?preview=true&p=1&typeName=Post /preview?preview=true&p=1&typeName=Post 🟢
Published /fr/posts/hello-world?preview=true&p=1&typeName=Post /fr/preview?preview=true&p=1&typeName=Post 🟢

After

Status Preview URL Result
Draft /?preview=true&p=1&typeName=Post /preview?preview=true&p=1&typeName=Post 🟢
Draft /en?preview=true&p=1&typeName=Post /preview?preview=true&p=1&typeName=Post 🟢
Draft /fr?preview=true&p=1&typeName=Post /fr/preview?preview=true&p=1&typeName=Post 🟢
Published /posts/hello-world?preview=true&p=1&typeName=Post /preview?preview=true&p=1&typeName=Post 🟢
Published /fr/posts/hello-world?preview=true&p=1&typeName=Post /fr/preview?preview=true&p=1&typeName=Post 🟢

@changeset-bot
Copy link

changeset-bot bot commented Apr 27, 2022

🦋 Changeset detected

Latest commit: 4ddc114

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@faustjs/next Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@apmatthews apmatthews changed the title Make preview links compatible with i18n config Fixes draft preview links when i18n is enabled in Next.js config Apr 28, 2022
@apmatthews apmatthews marked this pull request as ready for review April 28, 2022 15:28
@apmatthews apmatthews requested a review from a team as a code owner April 28, 2022 15:28
Copy link
Member

@theodesp theodesp left a comment

Choose a reason for hiding this comment

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

LGTM

@apmatthews apmatthews merged commit da3a1ae into canary Apr 29, 2022
@apmatthews apmatthews deleted the merl-220-i18n-previews branch April 29, 2022 15:14
@github-actions github-actions bot mentioned this pull request Apr 29, 2022
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