@@ -42,7 +42,7 @@ class ResourceMapper {
4242 }
4343
4444 // Maps the request for a given resource and representation format to a server file
45- // When searchIndex is true and the URL ends with a '/', indexFilename will be matched.
45+ // When searchIndex is true and the URL ends with a '/', and contentType includes 'text/html' indexFilename will be matched.
4646 async mapUrlToFile ( { url, contentType, createIfNotExists, searchIndex = true } ) {
4747 let fullPath = this . getFullPath ( url )
4848 let isIndex = searchIndex && fullPath . endsWith ( '/' )
@@ -53,7 +53,9 @@ class ResourceMapper {
5353 if ( createIfNotExists && contentType !== this . _indexContentType ) {
5454 throw new Error ( `Index file needs to have ${ this . _indexContentType } as content type` )
5555 }
56- fullPath += this . _indexFilename
56+ if ( contentType && contentType . includes ( this . _indexContentType ) ) {
57+ fullPath += this . _indexFilename
58+ }
5759 }
5860 // Create the path for a new file
5961 if ( createIfNotExists ) {
@@ -69,15 +71,7 @@ class ResourceMapper {
6971 const folder = fullPath . substr ( 0 , fullPath . length - filename . length )
7072
7173 // Find a file with the same name (minus the dollar extension)
72- let match = searchIndex ? await this . _getMatchingFile ( folder , filename , isIndex ) : ''
73- // Do not match with html if the content type won't allow it
74- if (
75- contentType &&
76- ! ( contentType . includes ( 'text/html' ) || contentType . includes ( '*/*' ) ) &&
77- match === this . _indexFilename
78- ) {
79- match = ''
80- }
74+ let match = searchIndex ? await this . _getMatchingFile ( folder , filename , isIndex , contentType ) : ''
8175 if ( match === undefined ) {
8276 // Error if no match was found,
8377 // unless the URL ends with a '/',
@@ -95,13 +89,13 @@ class ResourceMapper {
9589 return { path, contentType : contentType || this . _defaultContentType }
9690 }
9791
98- async _getMatchingFile ( folder , filename , isIndex ) {
92+ async _getMatchingFile ( folder , filename , isIndex , contentType ) {
9993 const files = await this . _readdir ( folder )
10094 // Search for files with the same name (disregarding a dollar extension)
10195 if ( ! isIndex ) {
10296 return files . find ( f => this . _removeDollarExtension ( f ) === filename )
10397 // Check if the index file exists
104- } else if ( files . includes ( this . _indexFilename ) ) {
98+ } else if ( files . includes ( this . _indexFilename ) && contentType && contentType . includes ( this . _indexContentType ) ) {
10599 return this . _indexFilename
106100 }
107101 }
0 commit comments