Skip to content

Commit

Permalink
Go back to the deprecated url.parse
Browse files Browse the repository at this point in the history
While deprecated, it's much cleaner to use this when we have relative URLs. It also appears to be a lot faster.

Also, there's an open issue about this in Node.js nodejs/node#12682
  • Loading branch information
XhmikosR committed Oct 16, 2019
1 parent a4fbaf2 commit 5311fd1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
7 changes: 3 additions & 4 deletions routes/appendLocals.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const path = require('path');
const url = require('url');
const { generateSri } = require('../lib/helpers');
const { app, files } = require('../config');

Expand Down Expand Up @@ -65,11 +66,9 @@ function capitalize(str) {
function appendLocals(req, res) {
const siteUrl = getCurrentSiteurl(req);
const pageUrl = req.originalUrl;
// OK, hack-ish way...
const pathname = pageUrl.split('?')[0];
const canonicalUrl = new URL(pathname, app.siteurl);
const canonicalUrl = url.resolve(app.siteurl, url.parse(pageUrl).pathname);
const theme = getThemeQuery(req);
const bodyClass = generateBodyClass(pathname);
const bodyClass = generateBodyClass(pageUrl);

const locals = {
siteUrl,
Expand Down
3 changes: 2 additions & 1 deletion test/test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const ENV = process.env;
ENV.BCDN_HEADERS = ENV.BCDN_HEADERS || 'production';

const assert = require('assert').strict;
const url = require('url');
const escapeStringRegexp = require('escape-string-regexp');
const { htmlEncode } = require('htmlencode');
const mockDate = require('mockdate');
Expand Down Expand Up @@ -141,7 +142,7 @@ function assertPageHeader(txt, res, cb) {
}

function assertCanonicalUrl(str, res, cb) {
const expected = `<link rel="canonical" href="${new URL(str, config.siteurl)}">`;
const expected = `<link rel="canonical" href="${url.resolve(config.siteurl, str)}">`;

assert.ok(res.body.includes(expected), 'Expects page to have a canonical URL');
cb();
Expand Down

0 comments on commit 5311fd1

Please sign in to comment.