Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react-router-dom status code 301

// client
<BrowserRouter>
  <App/>
</BrowserRouter>

// server (not the complete story)
<StaticRouter
  location={req.url}
  context={context}
>
  <App/>
</StaticRouter>
Comment

react-router-dom status code 301

const context = {};
const markup = ReactDOMServer.renderToString(
  <StaticRouter location={req.url} context={context}>
    <App />
  </StaticRouter>
);

if (context.url) {
  // Somewhere a `<Redirect>` was rendered
  redirect(301, context.url);
} else {
  // we're good, send the response
}
Comment

react-router-dom status code 301

function RedirectWithStatus({ from, to, status }) {
  return (
    <Route
      render={({ staticContext }) => {
        // there is no `staticContext` on the client, so
        // we need to guard against that here
        if (staticContext) staticContext.status = status;
        return <Redirect from={from} to={to} />;
      }}
    />
  );
}

// somewhere in your app
function App() {
  return (
    <Switch>
      {/* some other routes */}
      <RedirectWithStatus status={301} from="/users" to="/profiles" />
      <RedirectWithStatus
        status={302}
        from="/courses"
        to="/dashboard"
      />
    </Switch>
  );
}

// on the server
const context = {};

const markup = ReactDOMServer.renderToString(
  <StaticRouter context={context}>
    <App />
  </StaticRouter>
);

if (context.url) {
  // can use the `context.status` that
  // we added in RedirectWithStatus
  redirect(context.status, context.url);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: nestjs forRoutes middlewarwe 
Javascript :: find NaN in js return index 
Javascript :: cant resolve module after typescript install 
Javascript :: how to pass data to ejs partials 
Javascript :: usescript +react 
Javascript :: how to get data from jsonplaceholder 
Javascript :: id generator using javascript 
Javascript :: invite tracker node.js v13 
Javascript :: Working with substring 
Javascript :: date pretty print javascript 
Javascript :: Mutations 
Javascript :: javascript check if valid url 
Javascript :: nestjs openapi yaml file 
Javascript :: angular deployment 
Javascript :: mreact graph 
Javascript :: nodejs spawn detached command 
Javascript :: install reactivesearch 
Javascript :: Decimal Base Exponent shorthand javascript 
Javascript :: taylors javascript test 
Javascript :: put validation on the cell number in angular 
Javascript :: trigger many url calls JavaScript 
Javascript :: &lt;Link&gt; react import 
Javascript :: gitignore jsconfig 
Javascript :: jquery switch css style sheets 
Javascript :: remove all special characters online 
Javascript :: Using a fallback if module loading fails 
Javascript :: disabling first item in dropdownlist 
Javascript :: joi validation error message in path parameter value array to string 
Javascript :: add and remove multiple markers on google maps js 
Javascript :: Example of Promise.any() and AggregateError in es12 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =