Skip to content

Commit

Permalink
check if main document is redirected [#565]
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb committed Oct 6, 2023
1 parent cf7ce98 commit 227a5ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/file.js
Expand Up @@ -786,10 +786,19 @@ export async function vinylize(src, options = {}) {
} else if (filepath && isVinyl(filepath)) {
return filepath;
} else if (filepath && isRemote(filepath)) {
let url = filepath;
try {
const response = await fetch(filepath, {options, request: {method: 'head'}});
if (response.url !== url) {
debug(`(vinylize) found redirect from ${url} to ${response.url}`);
url = response.url;
}
} catch {}

file.remote = true;
file.url = filepath;
file.urlObj = urlParse(filepath);
file.contents = await fetch(filepath, options);
file.url = url;
file.urlObj = urlParse(url);
file.contents = await fetch(url, options);
file.virtualPath = file.urlObj.pathname;
} else if (filepath && fs.existsSync(filepath)) {
file.path = filepath;
Expand Down
6 changes: 3 additions & 3 deletions test/file.test.js
Expand Up @@ -279,7 +279,7 @@ test('Compute document base (with base option)', async () => {
{filepath: `http://localhost:${port}/folder/generate-default.html`, expected: '/folder'},
{filepath: `http://localhost:${port}/folder/head.html`, expected: '/folder'},
{filepath: `http://localhost:${port}/generate-default.html`, expected: '/'},
{filepath: `http://localhost:${port}/folder`, expected: '/'},
{filepath: `http://localhost:${port}/folder`, expected: '/folder'},
{filepath: `http://localhost:${port}/folder/`, expected: '/folder'},
{filepath: path.join(__dirname, 'fixtures/folder/subfolder/head.html'), expected: '/folder/subfolder'},
{filepath: path.join(__dirname, 'fixtures/folder/generate-default.html'), expected: '/folder'},
Expand All @@ -306,7 +306,7 @@ test('Compute document base (with base option)', async () => {
test('Compute document base (without base option)', async () => {
const vinyls = await Promise.all(
[
{filepath: `http://localhost:${port}/folder`, expected: '/'},
{filepath: `http://localhost:${port}/folder`, expected: '/folder'},
{filepath: `http://localhost:${port}/folder/`, expected: '/folder'},
{filepath: path.join(__dirname, 'fixtures/folder/subfolder/head.html'), expected: '/folder/subfolder'},
{filepath: path.join(__dirname, 'fixtures/folder/generate-default.html'), expected: '/folder'},
Expand Down Expand Up @@ -339,7 +339,7 @@ test('Get document', async () => {
});

const tests = [
{filepath: `http://localhost:${port}/folder`, expected: '/folder'},
{filepath: `http://localhost:${port}/folder`, expected: '/folder/index.html'},
{filepath: `http://localhost:${port}/folder/`, expected: '/folder/index.html'},
{filepath: path.join(__dirname, 'fixtures/folder/subfolder/head.html'), expected: '/folder/subfolder/head.html'},
{
Expand Down

0 comments on commit 227a5ba

Please sign in to comment.