question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Directory with `index.html` should serve welcome file without redirect

See original GitHub issue

Actual behavior (the bug) When you request a directory path /path/foo and a welcome file exists /path/foo/index.html, the response is a 302 redirect to /path/foo/.

Expected behavior The expectation is that the browser keeps /path/foo and serves the content of /path/foo/index.html.

To Reproduce In this unit test:

https://github.com/tipsy/javalin/blob/24c4532636945ac832adabb69846d863affead1e/javalin/src/test/java/io/javalin/TestStaticDirectorySlash.kt#L19-L23

Disabling the http test client’s handling of redirects will demonstrate this. E.g.:

 @Test 
 fun `normal javalin ignores static directory slashes`() = TestUtil.test(normalJavalin) { _, http -> 
     http.disableUnirestRedirects()
     assertThat(http.getBody("/subpage")).isEqualTo("TEST") // <-- this will fail now since a 302 is returned
     assertThat(http.getBody("/subpage/")).isEqualTo("TEST")
 } 

Additional context

  • In our use case we are using vite (with svelte) which generate static files for a portion of our site, and vite’s output is organized with one page per directory, with an index.html being the entry point for each page. An additional Javalin static file handler is configured to serve that all that content.
  • One might argue this is merely an aesthetic issue between showing /path/foo vs /path/foo/ in the browser bar. I place a high value on such aesthetics for our users. 😄
  • This is relevant only for static content, and I think #1002 may have aimed to fix this, but not sure. Maybe that PR was just to accomplish serving the index.html content, even if it meant doing an intermediate redirect. @AlexGustafsson do your /path/foo urls get redirected to /path/foo/ after your PR?
  • We are still using newest Javalin 3.x.x for the foreseeable future. @tipsy Are you still accepting PRs for the 3.x.x line? If I get that working, I could look doing at another PR for 4.x.x.

This seems to be fixable – I have pinpointed where Jetty makes the decision to do a redirect vs send back the welcome file. It comes down to how Jetty uses a PathResource with or without a trailing slash. So that’s a first step toward knowing what to pass into the Jetty function(s) to get the desired behavior. Here’s where Jetty makes its choice:

https://github.com/eclipse/jetty.project/blob/5eb7b70df7d1e25ffb4ce267126c122f94c181fd/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java#L377-L401

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bseibcommented, Jan 7, 2022

Did you figure everything out @bseib ?

On a good trajectory now I think with a RewriteHandler and parsing the manifest.json that vite produces to know the endpoints. Sorry for the “noise”. 😄

1reaction
bseibcommented, Jan 5, 2022

There should only be one 3x branch 🤔

Whoops! I got confused by some tags I was looking at! I see the javalin-3x branch. I’m at the end of my day – I’ll probably send something tomorrow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Directory with index.html should serve welcome file without ...
When you request a directory path /path/foo and a welcome file exists /path/foo/index.html , the response is a 302 redirect to /path/foo/ ....
Read more >
How to redirect the home page of the site to /public/index.html ...
my problem is wen I run the project and enter the WEBSITE_NAME the index.html file contents appearing on the page but without loading...
Read more >
How to Change your Default Index Page in the htaccess file
First the server will check for first.html, if it does not find a file with that name, it continues to index.htm and so...
Read more >
Module mod_dir - IBM
The module mod_dir provides "trailing slash" redirects and serving directory index files. The index of a directory can come from one of two...
Read more >
How does the "/" automatically result in the root relative ...
In the relative roots video, we learned that using "/" can display index.html's page from another file. How does "/" automatically redirect ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found