After we upgraded to Drupal 10.1 we also changed the nginx configuration as the release notes mentioned. But some cases we found that when the asset aggregation is enabled the page does not appeared properly in the browser. We found Chrome 114 "Refused to apply style ... because its MIME type ('text/plain') or ('text/troff') is not a supported stylesheet MIME type, and strict MIME checking is enabled."
When I've cleared the generated files, the page is appeared properly. But when i refresh the page, the browser got these assets with the wrong Content type header.
If I modify the AssetControllerBase::deliver method, the add "Content-type" header the probleam disapears.
| Comment | File | Size | Author |
|---|---|---|---|
| #20 | 3371358-19.patch | 3.75 KB | catch |
| #16 | interdiff.txt | 819 bytes | catch |
| #16 | 3371358-16.patch | 3.7 KB | catch |
| #12 | 3371358-12.patch | 3.56 KB | catch |
| #12 | 3371358-12-test-only.patch | 2.53 KB | catch |
Comments
Comment #2
tikaszvince commentedComment #3
smustgrave commentedThank you for reporting
As a bug though we will need a test case showing the issue also.
Comment #4
tikaszvince commentedAttached a modified patch which adds a new assert to
Drupal\FunctionalTests\Asset\AssetOptimizationTest::assertAggregateto check response always deliverscontent-typeheader.Comment #5
catchShouldn't this be assertArrayHasKey?
Comment #6
tikaszvince commentedSure. Updated patch is attached.
Comment #7
catchAdding a test-only patch.
Comment #8
catchThe patch doesn't fail, I think this is because the test doesn't execute the 'from existing file' code path which should only really happen during stampedes.
Probably requires something like this:
We'd need to file_get_contents() the aggregate, call $this->flushCaches() to delete all aggregate files etc., then manually write back the file to it's original location with file_put_contents(), then request the aggregate URL again with the browser. That is equivalent to a stampede/race condition where another process has created the file after the asset request happened but before we check if the file exists.
Comment #9
smustgrave commented#8, if I'm reading correctly. Sounds like the tests need updating. Though seems like a lot but that's just me.
Comment #10
catchI just tried to write a test for #8, and just as it was nearly there, I realised it's impossible.
If we clear the cache then write the file, the file will exist on disk, and then when we request it with the browser, because it exists on disk, the webserver will just serve the file directly bypassing the controller. So it really is only possible to reproduce in the race condition/stampede case and I can't think how to simulate that - i.e. we'd have to request the controller with no file, then before it tries to create the aggregate, create the file underneath it.
The extra assertion added in #6 doesn't do any harm though, it ensures we're getting a content type from the other two cases, so I think that makes #6 RTBC.
This whole issue is a workaround for #3172550: Register Drupal's mime type guesser the Symfony MimeTypes service because this all ought to be handled automatically, so I've added a @todo pointing to that, but otherwise the patch is identical to #6. Once we fix #3172550: Register Drupal's mime type guesser the Symfony MimeTypes service and remove the workaround, this shouldn't need explicit test coverage anyway.
Comment #11
catchFigured out a way.
We can't reproduce the exact race condition, but routes are case insensitive in Drupal - so if we capitalise the directory, then we hit the 'race condition' code path because that will hit PHP and also find the correct file. It is a hack, but I don't see another way to get coverage.
Comment #12
catchWithout the cs error.
Comment #14
smustgrave commentedGoing to rely on the fail/pass from #12.
Comment #15
larowlanI think this is missing the argument name? Second arg is a boolean, so this should likely be
content_type: 'text/css'I think strict types would have caught this
Comment #16
catchIt's a bit early and took me a minute to figure out that 'strict types' meant 'declare(strict_types=1)' and not 'having a type hint'. I don't think we're using named arguments yet anywhere, so steering clear of that, although yes this is a case where they make sense.
Comment #17
glynster commentedRTBC +1 @catch #16 this resolves the issue for us.
Comment #18
smustgrave commented#15 appears to be addressed so remarking.
Comment #19
catchRe-rolled for fuzz after #3376263: Tighten xpath selectors to decrease complexity in tests
Comment #20
catchComment #23
larowlanCommitted cccf3c6 and pushed to 11.x. Thanks!
Backported to 10.1.x