I want to develop multiple page applications.Is it needed to load all the scripts and css file in each html file?
or We need to manage it in single index file itself ?
Do we need to write all pages in single index.html file using separate div with data-role="page" or we can create multiple html file for each page?
Yes phonegap has single page model, single page is good option over multiple page application with phonegap.But you can create it on both ways if you are going with multiple page application and having multiple html pages then also it's not required to load all the css and js stuff in every html pages you can declare it once and then you can use that js and css in all other pages as well.
Multi-Page Apps
Multi-page applications function more like traditional web pages. Each “page” or HTML file contains a finite and discrete set of functionality, and has limited client-side dynamic updates. A page is loaded, content is displayed, the user interacts with it, and then another page is loaded. These pages can be loaded from the local file system or from remote servers. This style of architecture works for many developers, however there are three notable drawbacks to this approach.
First, there is the transition between pages. Not only are there visual display artifacts when pages are loaded and unloaded, but you also lose anything in-memory in JavaScript. This includes framework libraries, collected data, and so on.
Single Page Apps
This approach enables you to create web apps that feel more like apps instead of web pages. Once the PhoneGap js library has been initialized, you never have to re-initialize it. By leveraging the single-page architecture, you will not lose data that is resident in memory, and you can manage the transition of content from one visual state to another. Many MVC/development frameworks leverage this approach for building applications. However, if you are leveraging the web view’s history management, then you have to manually manage URL fragments and history behaviors.
Related
What are good practices about building a multiple page application using modern JS frameworks?
Multiple page application
In multiple page application we have multiple templates using some “template syntax” to provide us with backend data and AJAX (if needed) or advanced UX voodoo is often handled by jQuery.
Single page application
In single page application “backend data” is provided by AJAX requests and whole routing, frontend logic is handled by JS. We often use some JS framework like Angular or React and compile our sources with task runners/bundlers like webpack or gulp.
Hybrid application
But the most popular around the web seems to be hybrid app. What is typical build workflow while working with such an app? I could not find any tutorials or guides.
So to be specific. I imagine webapp where in which, each page has to be compiled and could share some resources. Every page has own JS routing like wizards or subcomponents. Data is loaded both during page load and AJAX.
For example my webapp would have 3 pages:
guest page - would provide website user with limited content and attract him to sign up
user - would provide signed website user with full content, resources would be extended guest content
admin - shares only styles and webapp “core”
Task Runners/Bundlers
For example in webpack is there a way to specify multiple entry and output points? Maybe the better way is to have multiple webpack/gulp configurations. In that case If I have a lot of pages I would have to write webpack/gulp configurations for every page even though some of them could be exactly the same. How to run that kind of build?
Sharing resources
Will browser load cached js bundle with the same hash like bundle.a2k4jn2.js within the same domain but different address? If so, how to specify such a behaviour in tools like webpack or gulp. I heard about CommonsChunkPlugin but not sure how to use it or even I’m looking at right direction.
Templates
What if I want to load some “backend” data not by AJAX but at the page loading. Of course every templating engine provides us with ability to write native code directly in html template like JSP or PHP. But what if some routing is handled by JS and “template tag” is not visible for page at initial loading i.e. template would not be compiled. Sometimes template engine in server and client could have the same special tag like Blade and Angular which can lead to conflicts.
Directory structure
I suppose that in hybrid app frontend and backend will be tightly coupled. Sharing JS in hybrid app could lead to very complicated imports (in es6 or html script tag). How to keep it simple.
Deploy
What about deploying an application? In java it’s easy because we just specify directories (compiled pages) in build tool (maven, gradle) which be copied to jar/war, but in PHP source code is not compiled how to keep “js source” away from production I could not imagine sensible resolution other than writing own batch/bash script
Summary
I have mentioned specific technologies and frameworks. But my question is about common approach to work with such an webapp rather than “how to do sth in that tool”. Although code examples would be greatly appreciated.
Their is a lot in this question, as a starting point you can define multiple entry points in webpack.
https://webpack.js.org/concepts/entry-points/
If you want to mix data loading between FE and BE then you really need to write an isomorphic JS application and use Node as your BE, otherwise you’ll end up writing everything twice in different languages and having once come across a project like that, trust me you really want to avoid that.
The other bit of this question on shared resources is best answered by WebPack’s bundle splitting which is made for what is being asked here
https://webpack.js.org/guides/code-splitting/
Not sure if I totally understand the question, but single-spa (yes it's redundant) is a tool that can be used to combine multiple apps (even if they are different frameworks) into one single page application. Link to the docs: https://single-spa.js.org/docs/getting-started-overview
Javascript frameworks like AnguarJs, BackboneJs, Emberjs that use to develop Single Page Applicaitons(SPA's) load lots of js files. Since these files cannot be unloaded can a application go into a situation that cause memory issues because of these js files because SPA's usually doesn't refresh the page.
For an example if application have multiple modules( eg : payroll, attendance,.. of a ERP ) SPA load js files specific to that module to browser when it get loaded. But when navigated to different module without refreshing the page previously loaded js files remain in memory. Imagining application has lots of modules like this is it possible in a certain time these js files cause memory problem ( not enough memory or corruption) ?
Keep in mind that you don't need to use a framework to create a SPA. It is true that at times Angular, React and other frameworks do create bloated code, but they also provide many useful functionality. So depending on the project you may or may not use them.
You can also load your code as modules, and load the modules when needed and unload them when you don't need them. So there is more to SPA's than just using a framework.
Things to remember before developing a Single Page Application
Try to use minified versions of JS and css files
Never use Single page approach if you are developing a large
application because when all html is renderd in single page then the
web application get heavier
For performance efficiency, is there a way to not include scripts for certain pages? I am currently using Ruby, specifically Shopify's application and on some pages, it isn't necessary to have scripts of jQuery as there are no functionalities that require them. From my understanding, the template page is given to every page which includes the scripts. Is there a way to disable them for certain pages?
U can use form sections in template and such sections can be filled from views that use those templates. Populating from each view can be a workaround. If not use a different layout all together.
You could use an amd loader like require.js or browserify to load in the scripts on the pages you need them. However, it's generally better practice to load all the scripts you need for your entire site in one hit, to reduce lots of http requests. That is better for performance because once the script has downloaded for page X, page Y will just load it from the client browsers cache anyway. It depends on how big your site is, but generally you create the site on the premise and hope that people will visit most of your pages.
I have the need to expand an asp.net 2.0 web application that I recently created. Currently there is a single page that contains some embedded HTML that is needed in a separate section of the application in a new page that I am working on. On this new page, I am going to be using the HTML multiple times throughout the page, mostly duplicating the HTML with JavaScript/jQuery. So I am not sure if I want to have the HTML in a JavaScript variable or hidden in the page and then call $.clone() on the HTML to dynamically recreate it.
Is there a way to have shared HTML files within an asp.net 2.0 forms web application so that I do not have to complete dual maintenance of code; I assume similar to the way that an MVC application would work (although I am not completely familiar with how MVC HTML works).
Create it as a User Control if it's only ever going to be deployed within a single project - otherwise you would have some duplication of effort with versioning. If that's the case, re-create it as a server control and emit the HTML/JS from there; server controls can be shared across projects from their own library. You'll only need to maintain one copy.
Lets say an MVC3 project has a number of JavaScript files and libraries. These are referenced by a shared layout view, at the bottom of this page. The idea being the page loads faster as the libraries are included after the page main content. There is also no in-line JavaScript in the views.
The project has a number of areas and each has a number of controllers. There is however just 1 JavaScript file that contains all of the custom scripting and events for the whole site. So code unique to a particular controller will be included for all page loads.
Is there a better way to structure the files or to segregate the code so only relevant scripting is executed?
Edit:
So a strategy of splitting 1 large script file, or to only have scripting execute that's relevant to the current context. NO use initializing lots of event handlers for elements that do not exist on a page. Even manually checking if each element exists beforehand is an overhead in site wide file.