Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storybook: Add mechanism to redirect moved stories (#59181)
* 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
1 parent
75e4175
commit 9d35ab5
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |