Skip to content

Commit

Permalink
Fix/issue 463 pretty urls in prodServer (#464)
Browse files Browse the repository at this point in the history
* task: remove the url redirect from serve lifecycle

* fix: remove redundant case

* fix: accomodate present files with .html in pathname

* fix: typo

* fix: correct path

* fix: combine html/prettified url case

* fix: small refactor
  • Loading branch information
hutchgrant authored and thescientist13 committed Apr 3, 2021
1 parent edb725c commit 9e42163
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions packages/cli/src/lifecycles/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,32 @@ function getProdServer(compilation) {
app.use(async ctx => {
// console.debug('URL', ctx.request.url);
const { outputDir } = compilation.context;
const { url } = ctx.request;

if (ctx.request.url.endsWith('/')) {
ctx.redirect(`http://localhost:8080${ctx.request.url}index.html`);
}

if (ctx.request.url.endsWith('.html')) {
const contents = await fsp.readFile(path.join(outputDir, ctx.request.url), 'utf-8');

if (url.endsWith('/') || url.endsWith('.html')) {
const barePath = url.endsWith('/') ? path.join(url, 'index.html') : url;
const contents = await fsp.readFile(path.join(outputDir, barePath), 'utf-8');
ctx.set('Content-Type', 'text/html');
ctx.body = contents;
}

if (ctx.request.url.endsWith('.js')) {
const contents = await fsp.readFile(path.join(outputDir, ctx.request.url), 'utf-8');
if (url.endsWith('.js')) {
const contents = await fsp.readFile(path.join(outputDir, url), 'utf-8');

ctx.set('Content-Type', 'text/javascript');
ctx.body = contents;
}

if (ctx.request.url.endsWith('.css')) {
const contents = await fsp.readFile(path.join(outputDir, ctx.request.url), 'utf-8');
if (url.endsWith('.css')) {
const contents = await fsp.readFile(path.join(outputDir, url), 'utf-8');

ctx.set('Content-Type', 'text/css');
ctx.body = contents;
}

// TODO break up into distinct font / icons / svg handlers, decouple from to assets/
if (ctx.request.url.indexOf('assets/')) {
const assetPath = path.join(outputDir, ctx.request.url);
if (url.indexOf('assets/')) {
const assetPath = path.join(outputDir, url);
const ext = path.extname(assetPath);
const type = ext === '.svg'
? `${ext.replace('.', '')}+xml`
Expand All @@ -118,7 +115,7 @@ function getProdServer(compilation) {
}
}

if (ctx.request.url.endsWith('.json')) {
if (url.endsWith('.json')) {
const contents = await fsp.readFile(path.join(outputDir, 'graph.json'), 'utf-8');

ctx.set('Content-Type', 'application/json');
Expand Down

0 comments on commit 9e42163

Please sign in to comment.