Riot.js SSR (Server side rendering) how to init scripts afterwards render - server-side-rendering

I have done a server render of an app. How to init server side rendered app after render happened?
e.g.:
require("#riotjs/ssr/register")();
const App = require("./app.riot").default;
const riotRender = require("#riotjs/ssr").default;
app.get('/app', function (req, res) {
res.render('../apps/apps/t/base', {
app: "some data"
});
});
And the server side just has riot compiler connected.
I did not get from the docs on how it should be done. Init a Riot component does not seem to help me.
E.g. UI.
<!doctype html>
<html lang="en">
<head>
<script src="/js/riot+compiler.min.js"></script>
</head>
<app>[Rendered HTML]</app>
Adding component and initialising it like with browser rendering did not help.

One needs to "#hydrate" the component.
Use the lib alongside with #ssr rendering lib in riot.
There is a good example about it.
https://github.com/riot/examples/tree/gh-pages/ssr
Somehow docs on Server side rendering do not mention it broadly.
Hope it helps someone else.

Related

How to entirely disable server-side rendering in next.js v13?

The documentation says that I can disable server-side rendering by adding 'use client' to the top of files that need to be rendered only client-side.
However, in practice, I've added this header to every file in the project, and I see that both Layout and Page are being rendered server-side.
I can confirm this with a simple page:
'use client';
export default () => {
console.log('SERVER RENDER (page)');
return (
<div>test</div>
);
};
I would expect next dev to not output "SERVER RENDER (page)", but it does.
You don't need to disable server-side rendering (SSR), as it's not enabled by default in Next.js.
It pre-renders every page unless instructed otherwise, using either Static Generation or SSR.
Static Generation. The HTML generated at build time and will be reused at every request.
SSR. The HTML is generated on each request.
Next.js uses Static Generation whenever possible.
In your example, SSR doesn't happen. You're seeing the SERVER RENDER (page) message in terminal because you run it in dev (next dev) mode. In dev mode there is no build done, so the pages are generated on the go. You won't see this message in production mode. See this for more details.
Using use client; directive doesn't change the above, it just tells Next.js that a component is a Client Component, which will still be pre-rendered.
It looks like even if a component is marked 'use client', it will still be pre-rendered.
Client Components enable you to add client-side interactivity to your application. In Next.js, they are prerendered on the server and hydrated on the client. You can think of Client Components as how Next.js 12 and previous versions worked (i.e. the pages/ directory).
https://beta.nextjs.org/docs/rendering/server-and-client-components#client-components
#Nikolai pointed this out correctly, but did not answer how to disable SSR.
However, now that we know that Next 13 behaves the same as 12, we can also apply the same hydration workaround that was used in previous versions.
The TLDR is that you want to wrap your layout in a component that conditionally renders the element based on whether it detects the browser environment, e.g.
const Dynamic = ({ children }: { children: React.ReactNode }) => {
const [hasMounted, setHasMounted] = useState(false);
useEffect(() => {
setHasMounted(true);
}, []);
if (!hasMounted) {
return null;
}
return <>{children}</>;
};
export default ({ children }: { children: React.ReactNode }) => {
return (
<html lang="en">
<head />
<body>
<Dynamic>{children}</Dynamic>
</body>
</html>
);
};
Obviously, make sure you know what you are doing. This is generally not desired behavior, though there are exceptions.

Script Component does not work in next.js

I'm working in Tiny Editor, it's necessary to define a key for the editor, following the documentation I can consult this key through the tag <script src = 'address', that's how it works, but when joining the Script component of the next .js (< Script src = 'address') I can't communicate with tiny anymore, has anyone been through this?
# It works
<script src='address' />
# Does not work
<Script src='address' />
doc: https://nextjs.org/docs/basic-features/script
If Script component is inside <Head> component, use it outside <Head>
Try in a different browser. It might be browser-specific issue
default value for the strategy prop is afterInteractive. with this, script is fetched and executed after the page is interactive. Use beforeInteractive for critical scripts that need to be fetched and executed before the page is interactive.
use onError props for debugging.
onError={(e) => {
console.error('Script failed to load', e)
}}

How to put a Blazor Wasm application into an HTML Custom Element?

I'm trying to do some alchemy here: I have a Blazor Wasm application that I try (for some reasons) to encapsulate into an HTML Custom Element.
So far I understood that the script _framework/blazor.webassembly.js is the bootstrap of the application.
However, if I put that script into the shadow root of my HTML Custom Element, the script seems executed, breakpoints are reached, but nothing related to the Blazor application happens: the dotnet.wasm is not loaded, no message, no error is shown.
So the question is: did someone tried to encapsulate a Blazor Wasm application into an HTML Custom Element? How can it be done, if it can?
Edit:
Here is the resulting DOM that I have so far:
<html lang="en">
<head>
<!-- Usual things omitted -->
</head>
<body>
<my-custom-element>
#shadowRoot
<div id="app"></div>
<script src="_framework/blazor.webassembly.js" type="text/javascript"></script>
</my-custom-element>
<!-- The shadowRoot and its content is generated by the custom element declared in this script -->
<script src="importMyCustomElement.js" type="text/javascript"></script>
</body>
</html>
A bit late, but currently this is now possible. Blazor now supports registering components that can be instantiated by Javascript. There's also experimental support for registering them as custom elements.
Here's the recent devblog for it
https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-6-rc-1/#blazor-custom-elements
You can get started by installing the pre-release nuget package
Microsoft.AspNetCore.Components.CustomElements
Then instead of adding a root component, you can do this instead.
// WASM sample
builder.RootComponents.RegisterAsCustomElement<Counter>("my-blazor-counter");
// Server-side sample
builder.Services.AddServerSideBlazor(options =>
{
options.RootComponents.RegisterAsCustomElement<Counter>("my-blazor-counter");
});
Add the following scripts on your index
<script src="_content/Microsoft.AspNetCore.Components.CustomElements/BlazorCustomElements.js"></script>
<script src="_framework/blazor.webassembly.js"></script>
With that you can now use those components as custom elements, which can be used either in another SPA framework or just plain html.
// Plain usage
<my-blazor-counter increment="1"></my-blazor-counter>
// React
let count = 1;
<my-blazor-counter increment={count}></my-blazor-counter>
This works in both server-side, and webassembly. It can also be a neat way of creating real-time web-components (e.g chat box) using Server side Blazor without having to deal with SignalR.
I am investigating this as well. Looks like you are missing <div id="app">
element. Notice the id="app" this is what Blazor looks for by default (see Program.cs of Blazor Webassembly app).

Why do my MVC application gives errors on a cloud server? [duplicate]

I have a simple jquery click event
<script type="text/javascript">
$(function() {
$('#post').click(function() {
alert("test");
});
});
</script>
and a jquery reference defined in the site.master
<script src="<%=ResolveUrl("~/Scripts/jquery-1.3.2.js")%>" type="text/javascript"></script>
I have checked that the script is being resolved correctly, I'm able to see the markup and view the script directly in firebug, so I must be being found. However, I am still getting:
$ is not defined
and none of the jquery works. I've also tried the various variations of this like $(document).ready and jQuery etc.
It's an MVC 2 app on .net 3.5, I'm sure I'm being really dense, everywhere on google says to check the file is referenced correctly, which I have checked and checked again, please advise! :/
That error can only be caused by one of three things:
Your JavaScript file is not being properly loaded into your page
You have a botched version of jQuery. This could happen because someone edited the core file, or a plugin may have overwritten the $ variable.
You have JavaScript running before the page is fully loaded, and as such, before jQuery is fully loaded.
First of all, ensure, what script is call properly, it should looks like
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
and shouldn't have attributes async or defer.
Then you should check the Firebug net panel to see if the file is actually being loaded properly. If not, it will be highlighted red and will say "404" beside it. If the file is loading properly, that means that the issue is number 2.
Make sure all jQuery javascript code is being run inside a code block such as:
$(document).ready(function () {
//your code here
});
This will ensure that your code is being loaded after jQuery has been initialized.
One final thing to check is to make sure that you are not loading any plugins before you load jQuery. Plugins extend the "$" object, so if you load a plugin before loading jQuery core, then you'll get the error you described.
Note: If you're loading code which does not require jQuery to run it does not need to be placed inside the jQuery ready handler. That code may be separated using document.readyState.
It could be that you have your script tag called before the jquery script is called.
<script type="text/javascript" src="js/script.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
This results as $ is not defined
Put the jquery.js before your script tag and it will work ;) like so:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/script.js"></script>
First you need to make sure that jQuery script is loaded. This could be from a CDN or local on your website. If you don't load this first before trying to use jQuery it will tell you that jQuery is not defined.
<script src="jquery.min.js"></script>
This could be in the HEAD or in the footer of the page, just make sure you load it before you try to call any other jQuery stuff.
Then you need to use one of the two solutions below
(function($){
// your standard jquery code goes here with $ prefix
// best used inside a page with inline code,
// or outside the document ready, enter code here
})(jQuery);
or
jQuery(document).ready(function($){
// standard on load code goes here with $ prefix
// note: the $ is setup inside the anonymous function of the ready command
});
please be aware that many times $(document).ready(function(){//code here}); will not work.
If the jQuery plugin call is next to the </body>, and your script is loaded before that, you should make your code run after window.onload event, like this:
window.onload = function() {
//YOUR JQUERY CODE
}
`
so, your code will run only after the window load, when all assets have been loaded. In that point, the jQuery ($) will be defined.
If you use that:
$(document).ready(function () {
//YOUR JQUERY CODE
});
`
the $ isn't yet defined at this time, because it is called before the jQuery is loaded, and your script will fail on that first line on console.
I just did the same thing and found i had a whole lot of
type="text/javacsript"
So they were loading, but no further hint as to why it wasn't working. Needless to say, proper spelling fixed it.
Use a scripts section in the view and master layout.
Put all your scripts defined in your view inside a Scripts section of the view. This way you can have the master layout load this after all other scripts have been loaded. This is the default setup when starting a new MVC5 web project. Not sure about earlier versions.
Views/Foo/MyView.cshtml:
// The rest of your view code above here.
#section Scripts
{
// Either render the bundle defined with same name in BundleConfig.cs...
#Scripts.Render("~/bundles/myCustomBundle")
// ...or hard code the HTML.
<script src="URL-TO-CUSTOM-JS-FILE"></script>
<script type="text/javascript">
$(document).ready(function () {
// Do your custom javascript for this view here. Will be run after
// loading all the other scripts.
});
</script>
}
Views/Shared/_Layout.cshtml
<html>
<body>
<!-- ... Rest of your layout file here ... -->
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/bootstrap")
#RenderSection("scripts", required: false)
</body>
</html>
Note how the scripts section is rendered last in the master layout file.
It means that your jQuery library has not been loaded yet.
You can move your code after pulling jQuery library.
or you can use something like this
window.onload = function(){
// Your code here
// $(".some-class").html("some html");
};
As stated above, it happens due to the conflict of $ variable.
I resolved this issue by reserving a secondary variable for jQuery with no conflict.
var $j = jQuery.noConflict();
and then use it anywhere
$j( "div" ).hide();
more details can be found here
make sure you really load jquery
this is not jquery - it's the ui!
<script language="JavaScript"
src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js">
</script>
This is a correct script source for jquery:
<script language="JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
Are you using any other JavaScript libraries? If so, you will probably need to use jQuery in compatibility mode:
http://docs.jquery.com/Using_jQuery_with_Other_Libraries
after some tests i found a fast solution ,
you can add in top of your index page:
<script>
$=jQuery;
</script>
it work very fine :)
I had the same problem and resolved it by using
document.addEventListener('DOMContentLoaded', () => {
// code here
});
I got the same error message when I misspelled the jQuery reference and instead of type="text/javascript" I typed "...javascirpt". ;)
It sounds like jQuery isn't loading properly. Which source/version are you using?
Alternatively, it could a be namespace collision, so try using jQuery explicitly instead of using $. If that works, you may like to use noConflict to ensure the other code that's using $ doesn't break.
That error means that jQuery has not yet loaded on the page. Using $(document).ready(...) or any variant thereof will do no good, as $ is the jQuery function.
Using window.onload should work here. Note that only one function can be assigned to window.onload. To avoid losing the original onload logic, you can decorate the original function like so:
originalOnload = window.onload;
window.onload = function() {
if (originalOnload) {
originalOnload();
}
// YOUR JQUERY
};
This will execute the function that was originally assigned to window.onload, and then will execute // YOUR JQUERY.
See https://en.wikipedia.org/wiki/Decorator_pattern for more detail about the decorator pattern.
I use Url.Content and never have a problem.
<script src="<%= Url.Content ("~/Scripts/jquery-1.4.1.min.js") %>" type="text/javascript"></script>
In the solution it is mentioned -
"One final thing to check is to make sure that you are not loading any plugins before you load jQuery. Plugins extend the "$" object, so if you load a plugin before loading jQuery core, then you'll get the error you described."
For avoiding this -
Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.noConflict():
I had this problem once for no apparent reason. It was happenning locally whilst I was running through the aspnet development server. It had been working and I reverted everything to a state where it had previously been working and still it didn't work. I looked in the chrome debugger and the jquery-1.7.1.min.js had loaded without any problems. It was all very confusing. I still don't know what the problem was but closing the browser, closing the development server and then trying again sorted it out.
Just place jquery url on the top of your jquery code
like this--
<script src="<%=ResolveUrl("~/Scripts/jquery-1.3.2.js")%>" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$('#post').click(function() {
alert("test");
});
});
</script>
I had the same problem and it was because my reference to the jQuery.js was not in the tag. Once I switched that, everything started working.
Anthony
Check the exact path of your jquery file is included.
<script src="assets/plugins/jquery/jquery.min.js"></script>
if you add this on bottom of your page , please all call JS function below this declaration.
Check using this code test ,
<script type="text/javascript">
/***
* Created by dadenew
* Submit email subscription using ajax
* Send email address
* Send controller
* Recive response
*/
$(document).ready(function() { //you can replace $ with Jquery
alert( 'jquery working~!' );
});
Peace!
This is the common issue to resolve this you have to check some point
Include Main Jquery Library
Check Cross-Browser Issue
Add Library on TOP of the jquery code
Check CDNs might be blocked.
Full details are given in this blog click here
I came across same issue, and it resolved by below steps.
The sequence of the scripts should be as per mentioned below
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script src="~/Scripts/jquery-ui.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
This sequence was not correct for my code, I corrected this as per the above and it resolved my issue of Jquery not defined.
We have the same problem....but accidentally i checked folder properties and set something...
You have to check the properties of each folders that you're accessing..
right click folder
'permissions' tab
set the folder access :
OWNER: create and delete files
GROUP: access files
OTHERS: access files
I hope that this is the solution......
When using jQuery in asp.net, if you are using a master page and you are loading the jquery source file there, make sure you have the header contentplaceholder after all the jquery script references.
I had a problem where any pages that used that master page would return '$ is not defined' simply because the incorrect order was making the client side code run before the jquery object was created. So make sure you have:
<head runat="server">
<script type="text/javascript" src="Scripts/jquery-VERSION#.js"></script>
<asp:ContentPlaceHolder id="Header" runat="server"></asp:ContentPlaceHolder>
</head>
That way the code will run in order and you will be able to run jQuery code on the child pages.
In my case I was pointing to Google hosted JQuery. It was included properly, but I was on an HTTPS page and calling it via HTTP. Once I fixed the problem (or allowed insecure content), it fired right up.
After tried everything here with no result, I solved the problem simply by moving the script src tag from body to head
I was having this same problem and couldn't figure out what was causing it. I recently converted my HTML files from Japanese to UTF-8, but I didn't do anything with the script files. Somehow jquery-1.10.2.min.js became corrupted in this process (I still have no idea how). Replacing jquery-1.10.2.min.js with the original fixed it.
it appears that if you locate your jquery.js files under the same folder or in some subfolders where your html file is, the Firebug problem is solved. eg if your html is under C:/folder1/, then your js files should be somewhere under C:/folder1/ (or C:/folder1/folder2 etc) as well and addressed accordingly in the html doc. hope this helps.
I have the same issue and no case resolve me the problem. The only thing that works for me, it's put on the of the Site.master file, the next:
<script src="<%= ResolveUrl("~/Scripts/jquery-1.7.1.min.js") %>" type="text/javascript"></script>
<script src="<%= ResolveUrl("~/Scripts/bootstrap/js/bootstrap.min.js") %>" type="text/javascript"></script>
With src="<%= ResolveUrl("")... the load of jQuery in the Content Pages is correct.

How to give javascript alert from web context(Iframe) in metro app.

In my metro app ..I am using iframe to load a web application - basically a form which contains some controls and finally user click on finish button and i want to show alert
i know in metro app. we can give the alert using "new Windows.UI.PopupMessage" to show the alert. but how can i do the same from the web context(Iframe).
i have a function (showalert();) in my default.js where i am using the "new Windows.UI.PopupMessage" to show messages. if i am trying to access this function from iframe page like "window.parent.showalert();". i get exception saying access denied.
Please someone reply to this as this is very critical for me.
thanks & regards
Goutham
You can use HTML 5's postMessage to communicate between contexts.
Below is an image of a simplistic example with the relevant code snippets following it; the Bing Maps trip optimizer example uses this same technique on a grander scale.
The main page (default.js), which is running in the local context, includes an IFRAME loaded in web context via the following markup (I left out the unchanged <head> element to save space):
<body onload="localContext.onLoad();">
<p style="margin-top: 150px">This is default.html in the local context</p>
<div style="background-color: azure; width: 300px">
<iframe src="ms-appx-web:///webpage.html" />
</div>
</body>
localContext is defined in default.js as
var localContext = {
onLoad: function () {
window.attachEvent("onmessage",
function (msg) {
if (msg.origin == "ms-appx-web://bfddc371-2040-4560-a61a-ec479ed996b0")
new Windows.UI.Popups.MessageDialog(msg.origin).showAsync().then();
});
}
};
and it defines an onLoad function for default.html that registers a listener to the onmessage event, and when that event fires a MessageDialog is shown (or you can take whatever action you want to do in the local context).
Note that the parameter to the message event callback (msg here) also includes a origin property that you can check to make sure you're only handling messages from expected senders.
The web page hosted in the IFRAME calls postMessage in the onclick event handler of a button (you'll probably want to pull the invocation a separate .js file versus in-line)
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
This is webpage.html loaded into an iFrame
<button id="button" onclick="window.parent.postMessage('Hello from Web Context', '*');">Say Hello</button>
</body>
</html>

Resources