Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve querystring when stripping file extension #144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,14 +551,17 @@ module.exports = async (request, response, config = {}, methods = {}) => {
const handlers = getHandlers(methods);

let relativePath = null;
let search = null;
let acceptsJSON = null;

if (request.headers.accept) {
acceptsJSON = request.headers.accept.includes('application/json');
}

try {
relativePath = decodeURIComponent(url.parse(request.url).pathname);
const parsed = url.parse(request.url);
relativePath = decodeURIComponent(parsed.pathname);
search = parsed.search || '';
} catch (err) {
return sendError('/', response, acceptsJSON, current, handlers, config, {
statusCode: 400,
Expand All @@ -584,7 +587,7 @@ module.exports = async (request, response, config = {}, methods = {}) => {

if (redirect) {
response.writeHead(redirect.statusCode, {
Location: encodeURI(redirect.target)
Location: encodeURI(`${redirect.target}${search}`)
});

response.end();
Expand Down
17 changes: 17 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,23 @@ test('set `cleanUrls` config property to `true` and try with file', async t => {
t.is(location, `${url}${target}`);
});

test('set `cleanUrls` config property to `true` and try with query', async t => {
const target = '/directory/clean-file';
const search = '?foo=bar/';

const url = await getUrl({
cleanUrls: true
});

const response = await fetch(`${url}${target}.html${search}`, {
redirect: 'manual',
follow: 0
});

const location = response.headers.get('location');
t.is(location, `${url}${target}${search}`);
});

test('set `cleanUrls` config property to `true` and not index file found', async t => {
const contents = await getDirectoryContents();
const url = await getUrl({cleanUrls: true});
Expand Down