Shubham Singh
12/01/2022, 5:17 PMBrowserRouter
working on my codebase. So I have created a new Kotlin/JS app and the only change I have made in the code is instead of directly rendering the welcome
screen, I am loading it inside a BrowserRouter
Like this:
createRoot(container).render(
BrowserRouter.create {
Routes {
Route {
path = "/login"
element = welcome
}
Route {
path = "*"
element = VFC { +"404 Not found!" }.create()
}
}
}
)
Now, the problem with the output of this code is that if I type <http://localhost:8080/login>
in my browser, it gives me the following error: Cannot GET /login
. Following are the only two entries I see in the console:
1. failed to load resource: the server responded with a status of 404 (Not Found)
2. DevTools failed to load source map: Could not load content for <chrome-extension://fjdmkanbdloodhegphphhklnjfngoffa/lib/browser-polyfill.min.js.map>: System error: net::ERR_BLOCKED_BY_CLIENT
Now the interesting thing is that, instead of a BrowserRouter
, if I use a HashRouter
and instead of entering <http://localhost:8080/login>
, I enter <http://localhost:8080/#/login>
in my browser, the same code starts working perfectly fine 👍 So I am not sure what changes do I need to make in order for the BrowserRouter
to work fine.
Any help would be greatly appreciated.Mike Dawson
12/01/2022, 6:30 PMShubham Singh
12/01/2022, 6:42 PMturansky
12/01/2022, 6:57 PMI assumed that I need to have an HTTP server that supports request rewriting etcYes, backend support required in such cases
Shubham Singh
12/01/2022, 7:37 PMYes, backend support required in such casesSorry for a basic question, can we do something in our case? Can we somehow try to make it work on our development machine as well?
turansky
12/01/2022, 7:42 PMwebpack-dev-server
?Shubham Singh
12/01/2022, 7:43 PMturansky
12/01/2022, 7:45 PMCan we somehow try to make it work on our development machine as well?How you run
localhost
? ./gradlew run -t
?Shubham Singh
12/01/2022, 7:46 PMbrowserDevelopmentRun
gradle taskShubham Singh
12/01/2022, 7:46 PMShubham Singh
12/01/2022, 7:49 PMturansky
12/01/2022, 7:56 PMI directly use theIt means, that you usegradle taskbrowserDevelopmentRun
webpack-dev-server
Shubham Singh
12/01/2022, 7:59 PMturansky
12/01/2022, 8:00 PMShubham Singh
12/01/2022, 8:00 PMturansky
12/01/2022, 8:01 PMturansky
12/01/2022, 8:02 PMturansky
12/01/2022, 8:04 PMwebpack.config.d
folder if you want static webpack configurationShubham Singh
12/01/2022, 8:05 PMShubham Singh
12/01/2022, 8:05 PMid "io.github.turansky.kfc.dev-server" version "5.71.0"
Shubham Singh
12/01/2022, 8:07 PMShubham Singh
12/03/2022, 6:25 AMplugins {
kotlin(Plugins.Kotlin.JS)
id("io.github.turansky.kfc.webpack") version "5.71.0"
}
group = App.Group
version = App.VersionName
repositories {
google()
maven("<https://maven.pkg.jetbrains.space/public/p/compose/dev>")
}
fun kotlinw(target: String): String = "org.jetbrains.kotlin-wrappers:kotlin-$target"
dependencies {
implementation(enforcedPlatform(kotlinw(Deps.Kotlin.JS.WrappersBOM)))
implementation(project(":common"))
implementation(kotlinw("react"))
implementation(kotlinw("react-dom"))
implementation(kotlinw("emotion"))
implementation(kotlinw("react-router-dom"))
}
kotlin {
js(IR) {
binaries.executable()
browser {
commonWebpackConfig {
cssSupport {
enabled = true
}
devServer = devServer?.copy(
open = false,
port = 3000
)
}
}
}
}
tasks {
patchWebpackConfig {
proxy("<http://localhost:3000/>")
}
}
I tried the library out
Maybe I’m doing something wrong
Because with the code above, when I try to access the app, I get this error:
<i> [webpack-dev-server] Content not from webpack is served from '/Users/shubhamsingh/IdeaProjects/play-together-kmp/web/build/processedResources/js/main' directory
<e> [webpack-dev-server] [HPM] Error occurred while proxying request localhost:3000/ to <http://localhost:3000/> [EAGAIN] (<https://nodejs.org/api/errors.html#errors_common_system_errors>)
<e> [webpack-dev-server] [HPM] Error occurred while proxying request localhost:3000/favicon.ico to <http://localhost:3000/> [ETIMEDOUT] (<https://nodejs.org/api/errors.html#errors_common_system_errors>)
I don’t know if this is helpful information or not, but this is part of a KMP project (so maybe I need to make some changes or include some plugins in the common gradle.kts as well?)Shubham Singh
10/24/2023, 2:09 PMTL;DR:
1. Create a package named webpack.config.d
in your Kotlin/JS module (i.e. it should be on the same level as the src
package), create a file inside it called webpack.js
, and paste the following line in it: config.devServer.historyApiFallback = true;
2. Paste the following code in your JS module's build.gradle.kts
file:
js(IR) {
browser {
runTask(
Action {
devServer = KotlinWebpackConfig.DevServer(
open = false,
port = 3000,
static = mutableListOf("${layout.buildDirectory.asFile.get()}/processedResources/js/main")
)
}
)
}
binaries.executable()
}
Here, only the static
line of code is important, you can ignore the other values