Comments (9)
Hi, thanks for reporting this. That is an interesting issue, not sure how to fix it though because after some initial investigation it seems like it is a level of caching in the razor engine. I'll have a closer look over the next few days and see what options I have to provide a better experience.
from giraffe.
Sounds about right. I'll see what I can do and try to come up with a pull request.
from giraffe.
Thanks for helping me with this project so much! I think this issue can be closed now :)
from giraffe.
I don't think it's the razor. I noticed similar problems with the htmlEngine as well.
If the razorView has a different model, the page does get a proper refresh.
let getText () =
let guid = Guid.NewGuid().ToString()
sprintf "Hello, Giraffe world! %s" guid
let webApp =
choose [
GET >=>
choose [
route "/" >=> razorView "Index.cshtml" { Text = getText () } ;
]
Is this an F# thing that if you call functions without arguments or without different argument values the result of the function is cached? Because I'm guessing in strict way calling the function with the same arguments should return the same value.
Are we missing a warbler
function somewhere?
from giraffe.
Hi, I think it is both. In the case of the html engine you need a warbler
, because functions in F# are eagerly evaluated and in the case of razor a warbler
will help to evaluate the function every time, but the razor engine will still compile a view only once and changing the cshtml
while the application is running will not have an effect.
Try this example:
let warbler f a = f a a
let getName() = Guid.NewGuid().ToString()
let webApp =
choose [
GET >=>
choose [
route "/razor" >=> razorHtmlView "Person" { Name = getName() }
route "/razor2" >=> warbler (fun _ -> razorHtmlView "Person" { Name = getName() })
]
setStatusCode 404 >=> text "Not Found" ]
When you run this application then /razor
will always return the same page, no matter what. In contrast /razor2
will return a different GUID every time, but both routes will not display any changes made to the cshtml
while the app is running.
from giraffe.
You're right. The warbler solves the execution issue but razor will still cache the changes.
Perhaps the razor issue should be tackled by configuring DotNetWatch
to restart if a *.cshtml
changes.
Today I also learned that the html engine should be a function instead of a value.
let index =
let currentTime = DateTime.Now.ToString("HH:mm:ss.ffffzzz")
let text = sprintf "Hello %s" currentTime
html [] [
head [] []
body [] [
h3 [] (rawText text)
]
]
let indexTwo() =
let currentTime = DateTime.Now.ToString("HH:mm:ss.ffffzzz")
let text = sprintf "Hello %s" currentTime
html [] [
head [] []
body [] [
h3 [] (rawText text)
]
]
Even with the warbler, the first index
result will only be executed once. Which makes sense.
from giraffe.
Yeah that's correct. So to sum this up, what do we think are the actions to take away here?
- Extend the SampleApp with the
DotNetWatch
to restart on*.cshtml
changes - Add a
warbler
function to the API - Document that the
HtmlEngine
views should be functions when it should not be static
Anything else?
from giraffe.
Hi, so this seems to work, even without a warbler, because it restarts the app on every cshtml change. You must start the app with dotnet watch run
though for it to work.
from giraffe.
Indeed, you only need the warbler if your model is dynamic.
Ex:
[<CLIMutable>]
type Person =
{
Name : string;
CurrentTime: DateTime;
}
route "/razor" >=> razorHtmlView "Person" { Name = "Razor"; CurrentTime = DateTime.Now }
@model SampleApp.Models.Person
@{
Layout = "_Layout";
}
<div>
<h3>Hello, @Model.Name</h3>
<p>Current time: @Model.CurrentTime</p>
</div>
<div>
@{
await Html.RenderPartialAsync("Partial");
}
</div>
from giraffe.
Related Issues (20)
- Migrate to System.Text.Json HOT 1
- Participate on hacktoberfest 2023? HOT 1
- Never decalre reader with `use` on `ctx.Request.Body` HOT 4
- Upgrade to .NET 8 HOT 6
- LinkGenerator doesn't work with routef HOT 1
- Giraffe 6.2 is breaks against Microsoft.IO.RecyclableMemoryStream 3.0.0 HOT 11
- Returning streams, either with WriteStreamAsync or WriteFileStreamAsync or their handler equivalents, is extremely slow HOT 4
- Question: How to approach outside in testing of a micro service? HOT 2
- Guid pattern in endpoint router matches invalid values, throws System.FormatException HOT 6
- Follow-up from "Minor code optimisation #567"
- Update SECURITY.md
- Start using fantomas to validate code submissions with CI HOT 6
- [README] Suggestion for "Getting Started"'s "Doing it manually" HOT 2
- [question] Would it be possible to make `Giraffe.EndpointRouting` case-sensitive? HOT 3
- Remove NuGet API key from the repository HOT 3
- 6.4.0 release references PR for updating to .NET 7 instead of .NET 8 HOT 1
- EndpointRouting - Create endpoint for multiple http verbs
- Request/discussion: WriteStreamAsync buffer size autotuning and/or increase default buffer size HOT 1
- Update `.vscode` debug configuration to point to existent projects
- Returning 406 when mustAccept fails HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from giraffe.