Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

External URL rewrites #832

Closed
benjaminkay93 opened this issue Dec 3, 2020 · 4 comments · Fixed by #839
Closed

External URL rewrites #832

benjaminkay93 opened this issue Dec 3, 2020 · 4 comments · Fixed by #839

Comments

@benjaminkay93
Copy link

Is your feature request related to a problem? Please describe.
Next.JS has as part of its core funcationality the ability to rewrite to external URL's.

In my particular example, we have our old servers running on a subdomain, as part of our migration we wish to point our www traffic to the subdomain if the route is not covered by next.js

Describe the solution you'd like

Respect the URL rewrite functionality of Next.JS

Describe alternatives you've considered

Having proxies to Next js, but that is now utililising all the joys of this component

Additional context
Readme hints this is being worked on, but the RFC has been closed #587

@dphang
Copy link
Collaborator

dphang commented Dec 3, 2020

Yes, I had left it out at the time as when I tried to implement it, there were some issues due to merging the external rewrite's response headers into CloudFront (blacklisted headers from CloudFront). Will try to work on it this week. I think we can use fetch API to request the rewritten web page.

@benjaminkay93
Copy link
Author

i had a few problems myself writting a local /api/graphql proxy to an external graphql service (to get around CORS), I've posted what I had here although I will admit it was very much a plaster until I learnt about rewrites / this feature is added to serverless-next.js, i had nothing for page rewrites to proxy though :(

import fetch from 'isomorphic-fetch';	

const ApolloProxy = async ({ body, headers }, res) => {	
  const requestHeaders = {	
    accept: 'application/json',	
  };	

  if (headers.authorization && headers.authorization !== 'undefined')	
    requestHeaders.authorization = headers.authorization;

  const apolloResponse = await fetch(process.env.APOLLO_SERVER_URL, {	
    method: 'POST',	
    body: JSON.stringify(body),	
    headers: {	
      ...requestHeaders,	
    },	
  });	

  const data = await apolloResponse.json();	
  res.send({ ...data });	
};	

export default ApolloProxy;

@patricktyndall
Copy link

Hey @dphang is this on the latest alphas? I tried on @sls-next/[email protected] and no luck. I double checked all your troubleshooting steps in the RFC as well.

@dphang
Copy link
Collaborator

dphang commented Dec 11, 2020

@patricktyndall It should be in 1.19.0-alpha.20. Not sure why it's not showing up in the GitHub releases page, maybe it failed to create the releases but it does show up in NPM. Anyway, I have also published another version (21) as there are a few dependency updates.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants