diff --git a/src/file.js b/src/file.js index 6cdab09..80fdcee 100644 --- a/src/file.js +++ b/src/file.js @@ -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; diff --git a/test/file.test.js b/test/file.test.js index a8a659c..8c0a51b 100644 --- a/test/file.test.js +++ b/test/file.test.js @@ -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'}, @@ -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'}, @@ -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'}, {