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

[V2] Implement dev server serveStaticFile #580

Merged
merged 6 commits into from
May 6, 2022
Merged
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
34 changes: 34 additions & 0 deletions packages/pwa-kit-dev/src/ssr/server/build-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,40 @@ export const DevServerMixin = {
})
},

serveStaticFile(filePath, opts = {}) {
return (req, res) => {
req.app.__devMiddleware.waitUntilValid(() => {
const options = req.app.options
const webpackStats = req.app.__devMiddleware.context.stats.stats

const serverCompilation = webpackStats.find(
// static files are copied into bundle
// in the server webpack config
(stat) => stat.compilation.name === SERVER
).compilation
const {assetsInfo} = serverCompilation
const assetInfo = assetsInfo.get(filePath)

// if the asset is not in the webpack bundle, then
// return 404, we don't care whether or not the file
// exists in the local file system
if (!assetInfo) {
res.sendStatus(404)
return
}
const {sourceFilename} = assetInfo
const sourceFilePath = path.resolve(sourceFilename)

res.sendFile(sourceFilePath, {
headers: {
'cache-control': options.defaultCacheControl
},
...opts
})
})
}
},

serveServiceWorker(req, res) {
req.app.__devMiddleware.waitUntilValid(() => {
const sourceMap = req.path.endsWith('.map')
Expand Down
56 changes: 56 additions & 0 deletions packages/pwa-kit-dev/src/ssr/server/build-dev-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,59 @@ describe('DevServer persistent caching support', () => {
})
})
})

describe('DevServer serveStaticFile', () => {
// This isn't ideal! We need a way to test the dev middleware
// including the on demand webpack compiler. However, the webpack config and
// the Dev server assumes the code runs at the root of a project.
// When we run the tests, we are not in a project.
// We have a /test_fixtures project, but Jest does not support process.chdir(),
// nor mocking process.cwd(), so we mock the dev middleware for now.
// TODO: create a proper testing fixture project and run the tests in the isolated
// project environment.
const MockWebpackDevMiddleware = {
waitUntilValid: (cb) => cb(),
context: {
stats: {
stats: [
{
compilation: {
name: 'server',
assetsInfo: new Map([
[
'static/favicon.ico',
{
sourceFilename: path.resolve(
testFixtures,
'app/static/favicon.ico'
)
}
]
])
}
}
]
}
}
}

test('should serve static files', async () => {
const options = opts()
const app = NoWebpackDevServerFactory._createApp(options)
app.__devMiddleware = MockWebpackDevMiddleware
app.use('/test', NoWebpackDevServerFactory.serveStaticFile('static/favicon.ico'))
return request(app)
.get('/test')
.expect(200)
})

test('should return 404 if static file does not exist', async () => {
const options = opts()
const app = NoWebpackDevServerFactory._createApp(options)
app.__devMiddleware = MockWebpackDevMiddleware
app.use('/test', NoWebpackDevServerFactory.serveStaticFile('static/IDoNotExist.ico'))
return request(app)
.get('/test')
.expect(404)
})
})
2 changes: 1 addition & 1 deletion packages/template-typescript-minimal/app/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const {handler} = runtime.createHandler(options, (app) => {
res.send()
})

app.get('/robots.txt', runtime.serveStaticFile('static/robots.txt'))
app.get('/favicon.ico', runtime.serveStaticFile('static/favicon.ico'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The remote server already has code to handle the favicon. So technically there are 2 handlers being setup for the when running remote, once here on line 43 of ssr.js and the second time inside the build-remote-server.js file below:

_setupCommonMiddleware(app, options) {

We should probably do something about that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also.. why are we removing the serving of the robots.txt file?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another reason why it might not make sense to have this line of code in ssr.js is because faviconPath is an option for the dev/remote servers. It would be odd to have that option but not do anything with it behind the scenes

Copy link
Collaborator Author

@kevinxh kevinxh May 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question... sorry i didn't explain that in my PR description.

Serving favicon is exactly the same as serving any static files. Since we are adding programmatic APIs like serveStaticFile and serveServiceWorker, I feel like it's better to keep things consistent, . If developers can already serve all static files including favicon via serveStaticFile, why do we need to have a specific configuration option faviconPath? It almost feels like the faviconPath adds unnecessary complexity to the users. And it feels wrong to have two approaches (configuration and programatic API) to basically do the same thing.

So, I want to propose that we remove faviconPath, and since @shethj is already working on the favicon ticket. I can work with @shethj to remove the option in his PR. I use favicon as an example in this PR, because that's the only static file in the template.

Copy link
Collaborator Author

@kevinxh kevinxh May 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also.. why are we removing the serving of the robots.txt file?

There is no robots.txt in the typescript template, so that endpoint is a 404, and I feel it's unnecessary for a "minimal" template to have robots.txt

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to have robots and favicon work the same way. So that is cool with me. You'd just have to make sure you remove all that special logic in the remote server factory.

But one thing that I'm curious about is your implementation of the serveStaticFile for the dev server factory. It's completely different from the implementation in the remote server factory and I wasn't expecting that, since I assumed that the only real issue was the fact that the build dir was set to build for remote and it should be something like `` for development. Lets have a small chat, later and se can talk about that a bit.


app.get('/worker.js(.map)?', runtime.serveServiceWorker)
app.get('*', runtime.render)
Expand Down