"Only absolute URLs are supported" after upgrading from 9.3.6 to 9.4 #12732
-
Good afternoon all! I appreciate any guidance you can offer as I'm eager to start using Next 9.4. I allow my users to login to my site via Discord. I've tested and code still works fine in Next 9.3.6. Doing an upgrade to 9.4 with absolutely no other changes cause the catch block in my code to fire off with the error message "Only absolute URLs are supported." I've done console.logs of my env vars and those are still pulling in correctly. I use a fetch library called ky, and so I thought that perhaps the fetch changes noted in 9.4 release notes could be at play, but removing ky and replacing with fetch did not fix the error. I'm hoping you all might be able to help me figure this out! redirectUri ends up looking like: export class Discord {
private clientId = process.env.DISCORD_CLIENT_ID;
private clientSecret = process.env.DISCORD_SECRET;
private headerCredentials = btoa(`${this.clientId}:${this.clientSecret}`);
public redirectUri = encodeURIComponent(`${BasePath.client}/auth`);
/**
* Grants a token to authenticate user logged in via Discord
* @param ctx NextPageContext
* @param code The code granted by logging in to Discord
*/
async grantToken(ctx: NextPageContext, code: string): Promise<BaseResponse> {
const { res } = ctx;
if (!res) return { cookie: null, maxAge: 0, successful: false };
const tokenEndpoint = `https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${this.redirectUri}`;
try {
// Get authorization token
const tokenResponse: TokenResponse = await ky
.post(tokenEndpoint, {
headers: {
Authorization: `Basic ${this.headerCredentials}`,
},
})
.json();
} catch (e) {
logger.error(`Could not get token response. ${e.message}`);
return { cookie: null, maxAge: 0, successful: false };
}
}
} Here is the try/catch block with fetch instead of ky: try {
// Get authorization token
const initialTokenResponse = await fetch(tokenEndpoint, {
method: 'POST',
headers: {
Authorization: `Basic ${this.headerCredentials}`,
},
});
const tokenResponse: TokenResponse = await initialTokenResponse.json();
} catch (e) {
logger.error(`Could not get token response. ${e.message}`);
return { cookie: null, maxAge: 0, successful: false };
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 10 replies
-
It has to do with how next handles env variables now. From the blog post:
So any env variables you want to expose to the browser now need to be prepended with NEXT_PUBLIC_ unless you define them in next.config.js. |
Beta Was this translation helpful? Give feedback.
-
Edited answer with a fix in case you need to continue using ky-universal Original answer I ended up doing the following and getting my project upgraded:
// Old
const response = await ky.post('my_absolute_path', { json: myRequestData }).json
// New
const data = await fetch('my_absolute_path', { method: 'post', body: JSON.stringify(myRequestData) });
const response = await data.json();
// Old
const { discordId } = req.body;
// New
const { discordId } = JSON.parse(req.body). I think step 3 is what I overlooked the first time and tripped me up. Putting this all here in case it helps someone else with this issue. |
Beta Was this translation helpful? Give feedback.
-
I'm facing the same issue, except I can't get rid of It works fine on Next 9.3.6 but breaks with this error on 9.4, without any changes to any other dependencies. For now, I've downgraded to 9.3.6 again. |
Beta Was this translation helpful? Give feedback.
-
And FWIW, when this error is thrown, the Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '[object%20Request]',
path: '[object%20Request]',
href: '[object%20Request]' } |
Beta Was this translation helpful? Give feedback.
Edited answer with a fix in case you need to continue using ky-universal
See discussion at #12761
Original answer
I ended up resolving this issue. It appears it was some conflict in the
ky-universal
library I was using coupled with the latest fetch changes in Next 4.0. I think in my initial attempt to convert to using fetch(), I must have used it wrong.I ended up doing the following and getting my project upgraded:
ky()
calls withfetch()
, and removed theky
andky-universal
packages entirely.