-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@W-17062545@ Add support for non-302 redirects (#2173)
* Add support for non-302 redirects * Changelog * Add javadoc * Code coverage * Alternate implementaiton * Apply feedback * Add rendering test * lint
- Loading branch information
1 parent
3725c0f
commit 50c0f74
Showing
5 changed files
with
104 additions
and
1 deletion.
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
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
36 changes: 36 additions & 0 deletions
36
packages/pwa-kit-react-sdk/src/ssr/universal/components/redirect-with-status/index.jsx
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,36 @@ | ||
/* | ||
* Copyright (c) 2024, Salesforce, Inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import React from 'react' | ||
import {Redirect, withRouter} from 'react-router-dom' | ||
import PropTypes from 'prop-types' | ||
|
||
/** | ||
* The `RedirectWithStatus` component is used to specify a different status code when redirecting via | ||
* the Redirect component. | ||
* The default redirect behavior when this component is not used is to set a 302 status. | ||
* | ||
* @param {number} status - The HTTP status code. Defaults to 302 if not specified | ||
* @param {object} staticContext - The router context | ||
* @param {string} to - The redirect's target path | ||
*/ | ||
const RedirectWithStatus = ({status = 302, staticContext, ...props}) => { | ||
// Handle server-side rendering | ||
if (staticContext) { | ||
staticContext.status = status | ||
} | ||
|
||
return <Redirect {...props} /> | ||
} | ||
|
||
RedirectWithStatus.propTypes = { | ||
status: PropTypes.number, | ||
staticContext: PropTypes.object, | ||
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]) | ||
} | ||
|
||
export default withRouter(RedirectWithStatus) |
44 changes: 44 additions & 0 deletions
44
packages/pwa-kit-react-sdk/src/ssr/universal/components/redirect-with-status/index.test.js
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,44 @@ | ||
/* | ||
* Copyright (c) 2024, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import React from 'react' | ||
import {render} from '@testing-library/react' | ||
import {Router, StaticRouter, Route} from 'react-router-dom' | ||
import {createMemoryHistory} from 'history' | ||
import RedirectWithStatus from './index' | ||
|
||
describe('RedirectWithStatus', () => { | ||
test('Redirects if no status or context is provided', () => { | ||
const targetUrl = '/target' | ||
const history = createMemoryHistory() | ||
history.push('/redirect') | ||
render( | ||
<Router history={history}> | ||
<Route path="/redirect"> | ||
<RedirectWithStatus to={targetUrl} /> | ||
</Route> | ||
</Router> | ||
) | ||
expect(history.location.pathname).toBe(targetUrl) | ||
}) | ||
test('Redirect renders with correct status', async () => { | ||
const context = {} | ||
const status = 303 | ||
const targetUrl = '/target' | ||
|
||
render( | ||
<StaticRouter location="/redirect" context={context}> | ||
<Route path="/redirect"> | ||
<RedirectWithStatus status={status} to={targetUrl} /> | ||
</Route> | ||
</StaticRouter> | ||
) | ||
|
||
expect(context.status).toBe(status) | ||
expect(context.url).toBe(targetUrl) | ||
}) | ||
}) |