-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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
feat(v2): docusaurus-plugin-client-redirects #2793
feat(v2): docusaurus-plugin-client-redirects #2793
Conversation
Deploy preview for docusaurus-2 ready! Built with commit 6b50763 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is an awesome feature, but for a complete understanding, I would like to see tests or/and draft doc.
Although we can make some improvements now.
packages/docusaurus-plugin-client-redirects/src/createRedirectPageContent.ts
Outdated
Show resolved
Hide resolved
packages/docusaurus-plugin-client-redirects/src/redirectCreators.ts
Outdated
Show resolved
Hide resolved
Thanks for the feedback @lex111 It's not ready and not tested, just wanted to let you see a preview of the API I'm crafting (actually didn't even run the code yet so it might have bugs) Will handle your feedbacks tomorrow. |
packages/docusaurus-plugin-client-redirects/src/createRedirectPageContent.ts
Outdated
Show resolved
Hide resolved
…resh header + tests
Hi @lex111 I've added the tests and made the code more robust, it's almost finished. Will finish tomorrow. When this feature is complete, using the following (main usecase) [
'@docusaurus/plugin-client-redirects',
{
fromExtensions: ['html'],
},
], Should redirect from But it should also support the "Watchman" case (#2697) that wants .html urls being the canonical ones: [
'@docusaurus/plugin-client-redirects',
{
toExtensions: ['html'],
},
], Should redirect from For power users, it's also possible to provide a function: [
'@docusaurus/plugin-client-redirects',
{
createRedirects: (existingPath) => {
return ["existingPath" + "/someFancyPathSuffix"];
},
},
], What do you think of this API? Also, a subtility is to make sure the user does not create "bad redirects" that would override existing html files, or having duplicate redirects. For now I just filter those, but maybe there's a good way to warn about those issues? |
So I think it's working fine now, and has good test coverage. I propose to dogfood on the docusaurus v2 to redirect /path.html to /path, so that we know it keeps working over time. It's already in the PR: https://deploy-preview-2793--docusaurus-2.netlify.app/docs/next/installation.html I'll try the plugin on the Watchman site now and see if I have any issue. And I'll write the doc. |
Hi @lex111 Here are some updates:
Here's the config deployed in the PR, so that we can test better on D2 website: [
'@docusaurus/plugin-client-redirects',
{
fromExtensions: ['html'],
redirects: [
{
to: '/',
from: [
'/plugin-client-redirects-tests/toHome1',
'/plugin-client-redirects-tests/toHome2',
],
},
{
to: '/docs',
from: '/plugin-client-redirects-tests/toDocs1',
},
{
to: '/docs',
from: '/plugin-client-redirects-tests/toDocs2',
},
{
to: '/docs',
from: '/plugin-client-redirects-tests/toHomeDuplicatePath',
},
],
createRedirects: function (existingPath) {
if (existingPath === '/') {
return [
'/',
'/docs',
'/plugin-client-redirects-tests/toHome3',
'/plugin-client-redirects-tests/toHome4',
'/plugin-client-redirects-tests/toHomeDuplicatePath',
];
}
},
},
], The following redirects work: Html extension redirects:
Other redirects:
(the weird url lowercasing is related to Netlify, that's annoying with caps in pathname, shouldn't happen on other hosts) As the config creates twice the Do you see other things to handle? |
About the version thing, I thought about it, and agree that's something we could support. It's already possible to add version redirects with the custom |
Honestly, this plugin really came out huge and strong I thought there would be much less code in order to implement it. We actually did not pay so much attention to error handling in our existing plugins. However, I can’t say that this is a bad approach, just before that we did not attach so much importance to this. I actually trust in a high test coverage of this plugin, since mentally this plugin is not easy to understand (I mean me). But this is really cool, although maybe we could simplify some things, for example, is it really necessary to use Yup? Although it may be necessary to go this way in our other plugins. I mean, maybe we should try to reconsider the development process of our plugins so that they are like this redirect plugin -- a lot of tests, a thorough validation of errors, a lot of high-level abstractions... Apparently I got carried away 😄 Okay, tomorrow I'll see it again in action and probably we can already merge it into master branch. P.S. But please add redirects for |
Hi @lex111
That's true, initially thought it would be smaller 😅 The size comes from giving more control to the user, and giving shortcuts for common patterns, with the fromExtensions/toExtensions.
That's true. I did more user input validation in this plugin than other plugins. Personally I'd rather validate all user inputs on all plugins, fail-fast, and give actionable error messages to the user whenever possible, as it would lead to less github issues on the long term. If you feel it's too much, not worth it, and not the direction we want to take, I'm ok with that and could remove all these validations.
I agree that it's not so easy to reason about this plugin, there are more hidden subtilities than I thought. Myself I often get confused, particularly inverting the from/to of the plugin. Maybe it's not the correct terminology to use, and we could find simpler wordings (like sourcePath/targetPath or something?) About the abstractions, I don't think there's a lot of them, the only thing I really abstracted is the "redirectCreator" concept, which is I can remove this abstraction if you feel it's too hard to reason about. That means I'll need to write less generic code here: function doCollectRedirects(pluginContext: PluginContext): RedirectMetadata[] {
const redirectsCreators: RedirectsCreator[] = buildRedirectCreators(
pluginContext.options,
);
const optionsRedirects = collectPluginOptionRedirects(pluginContext);
const redirectCreatorsRedirects = flatten(
redirectsCreators.map((redirectCreator) => {
return createRoutesPathsRedirects(redirectCreator, pluginContext);
}),
);
return [...optionsRedirects, ...redirectCreatorsRedirects];
} That would become something like: function doCollectRedirects(pluginContext: PluginContext): RedirectMetadata[] {
return [
...collectToExtensionsRedirects(pluginContext),
...collectFromExtensionsRedirects(pluginContext),
...collectPluginOptionRedirects(pluginContext),
...collectCreateRedirects(pluginContext),
];
} I think you are right, that may be simpler to understand, and this abstraction may not be worth it. Do you agree I should refactor? |
pluginContext.options.createRedirects, | ||
), | ||
]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lex111 after removal of createRedirects abstraction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yangshun can you review the docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this @slorber. Will leave it to you to self-merge.
@lex111 FYI I added a redirect from /introduction:
|
Finally merged ! @yangshun @lex111 should we make a release or we wait for the DocSearch V3 PR to be merged? I've seen it in the milestone here: https://github.com/facebook/docusaurus/milestone/9 |
@slorber aaaah! Why didn’t you squash all the commits into one when merging PR? I mean, this is the default option. Currently, in |
oooh nooo :/ not used to this workflow actually it's not the default option for me, will look to make it the default, sorry :/ |
So what do we do to fix this? |
@lex111 discussed with @yangshun and @JoelMarcey , it's not a big deal for this time and they enabled the option to only allow squash merge for the future they prefere to not force push to master (there's a security) sorry about that :/ |
If needed, I have a clean branch that could be force-pushed to master here: https://github.com/slorber/docusaurus/commits/master-backup |
I don't believe this causes much of a technical issue, requiring us to revert or force push. It might just make the commit log / history / etc a bit less clean. @lex111 Are you seeing issues either in this repo or your fork because of the merge commit (instead of the squash)? |
First draft of plugin to handle client side redirects
I tried to make it flexible, and testable (tests will come)
It should cover basic redirections with ease, like path.html -> path
It should also cover the Watchman case: path -> path.html
Not ready to run, but you can take a look and give me some early feedback @yangshun ?