Skip to content

Commit

Permalink
Storybook: Add mechanism to redirect moved stories (#59181)
Browse files Browse the repository at this point in the history
* Storybook: Add mechanism to redirect moved stories

* Simplify URL construction

Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: andrewhayward <andrewhayward@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
  • Loading branch information
4 people committed Feb 26, 2024
1 parent 75e4175 commit 9d35ab5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Expand Up @@ -16,6 +16,7 @@ import { useState } from '@wordpress/element';

const meta: Meta< typeof RadioGroup > = {
title: 'Components (Deprecated)/RadioGroup',
id: 'components-radiogroup',
component: RadioGroup,
// @ts-expect-error - See https://github.com/storybookjs/storybook/issues/23170
subcomponents: { Radio },
Expand Down
34 changes: 34 additions & 0 deletions storybook/manager-head.html
@@ -0,0 +1,34 @@
<script>
( function redirectIfStoryMoved() {
const REDIRECTS = [
{
from: /\/components-deprecated-/,
to: '/components-',
},
];

const params = new URLSearchParams( window.location.search );

const matchedRedirect = REDIRECTS.find( ( { from } ) =>
from.test( params.get( 'path' ) )
);

if ( ! matchedRedirect ) {
return;
}

params.set(
'path',
params
.get( 'path' )
.replace( matchedRedirect.from, matchedRedirect.to )
);

const { pathname, origin } = window.location;
// The decodeURIComponent keeps the slashes intact, to match how Storybook presents the `path` param.
const newUrl =
new URL( pathname, origin ) + '?' + decodeURIComponent( params );

window.location.replace( newUrl );
} )();
</script>

0 comments on commit 9d35ab5

Please sign in to comment.