5
votes

How do I route to longer URL paths with history mode?

example: /catalog/[product-name]/p/[product-id]

The site just goes blank with history mode on. With errors of the following type on my CSS and JS

Refused to execute script from '{URL}/catalog/[product-name]/p/dist/build.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

router code:

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/catalog/my-product-name/p/MASN123U', name: 'product', component: product }
  ]
})

Side note: It works fine without history mode, just that the URL reads

/#/catalog/[product-name]/p/[product-id]

Web.config is as per the docs

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="SPA Routes" stopProcessing="true">
                    <!-- match everything by default -->
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <!-- unless its a file -->
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <!-- or a directory -->
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <!-- or is under the /api directory -->
                        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
                        <!-- list other routes or route prefixes here if you need to handle them server side -->
                    </conditions>
                    <!-- rewrite it to /index.html -->
                    <action type="Rewrite" url="/index.html" />
                </rule>`enter code here`
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
1
Show you server-side configuration (url rewrite?). You have a problem there.acdcjunior
@acdcjunior added it.asdasdasdasdqwe

1 Answers

1
votes

From the error:

Refused to execute script from '{URL}/catalog/[product-name]/p/dist/build.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

Look somewhere in your HTML files, you probably have a script tag as such:

<script src="dist/build.js"></script>

When its path should be absolute, like:

<script src="/dist/build.js"></script>

Notice the / added before dist.