chrisvfritz / prerender-spa-plugin

Prerenders static HTML in a single-page application.
MIT License
7.32k stars 634 forks source link

Can't put html files routes that end in .html directly in the folder #39

Closed KenCorbettJr closed 7 years ago

KenCorbettJr commented 7 years ago

I would like to be able to have routes that end in .html so that the pre-rendered version can be a physical file. However, if I specify a route with a .html extension it puts the prerendered output in a folder with that name, in a file named index.html. Is there any way to have it just output the file directly without putting it in its own folder?

chrisvfritz commented 7 years ago

No, unfortunately. As mentioned in the README:

Only works with routing strategies using the HTML5 history API.

heinzdmx commented 7 years ago

I am running into a similiar issue. I am using Vue.js on S3. We want the following paths:

/ /somepath/sad /about /somepath/ads.html

The current code in index.js creates folders for a url and then a file named index.html. Unfortunately S3 forces redirects that causes /about to become /about/ in this case.

I have made a small change to index.js that enables us to have paths like this, I believe this is what @KenCorbettJr is trying to archive as well.

This code

        var folder = Path.join(self.staticDir, outputPath)
        mkdirp(folder, function (error) {
          if (error) {
            return reject('Folder could not be created: ' + folder + '\n' + error)
          }
          var file = Path.join(folder, 'index.html')
          FS.writeFile(
            file,
            prerenderedHTML,
            function (error) {
              if (error) {
                return reject('Could not write file: ' + file + '\n' + error)
              }
              resolve()
            }
          )
        })

Becomes

        var folder = Path.join(self.staticDir, outputPath.substr(0, outputPath.lastIndexOf("/")))
        var file = Path.join(folder, outputPath.substr(outputPath.lastIndexOf("/"), outputPath.length))

        if ('/' === outputPath.charAt(outputPath.length-1)) {
          file = Path.join(file, 'index.html')
        }

        function writeHTML() {
            FS.writeFile(
              file,
              prerenderedHTML,
              function (error) {
                if (error) {
                  return reject('Could not write file: ' + file + '\n' + error)
                }
                resolve()
              }
            )
        }

        if (file !== folder)
        {
          mkdirp(folder, function (error) {
            if (error) {
              return reject('Folder could not be created: ' + folder + '\n' + error)
            }

            writeHTML()
          })
        }
        else {
          writeHTML()
        }

If desired I can create a pull request with the code changes to enable this

KenCorbettJr commented 7 years ago

Thanks @heinzdmx for this snippet. At the very least I can add this to my own fork. You make a good point about the challenges in hosting this on S3. This library makes some assumptions about url rewrite rules that don't always apply. There may be instances where people have even changed their DirectoryIndex so that it serves a file with a different name other than index.html file when a request comes in to the folder. If that is the case this library would completely fail.

Even though it is an edge case, it would be nice if support was added to the library so that we wouldn't have to maintain our own fork, constantly merging in new code. It is a common enough use case that I think it makes sense. At the very least it gives this plugin the flexibility to work even when the hosting environment doesn't match the assumptions that were made. I think the current behavior is perfectly fine default, but it would be nice to have more options.

heinzdmx commented 7 years ago

@chrisvfritz Would you mind taking a look at my comment, to see if that is something you would be willing to add support for?