Prevent network request on every time input change - laravel-livewire

I am currently trying to prevent the network request that livewire sends on every time I type something in my input element but not able to figure out the exact reason so far.
I have one modal component and inside that, I have the input element like this. Even the input element has no wire:model or nothing just the plain input with bootstrap class.
<input type="text" class="form-control" />
I tried adding wire:ignore and everything but still I can see in the network tab that request is being sent every time when input changes and I want to prevent that.
Basically, I also have some elements where jQuery plugin select2 has been used with search feature and when I even search inside that generated search input with jQuery, network request keeps firing and select2 hides automatically.
Inside my livewire js hook i have added some code to initialize the select2 plugin after message is processed so that select2 is not broken but in this case I don;t want to send request at all.
document.addEventListener("DOMContentLoaded", () => {
Livewire.hook('message.processed', (message, component) => {
initSelect2(component);
})
});
How can I prevent all network requests that are firing back and forth between frontend and backend for any type of input element?

You can use wire:model.lazy="yourModel", this will result in updates (network requests) only happening when the user click away from the input field.
There's also wire:model.defer="yourModel" which will first trigger when you e.g. do a wire:click on a button.
Lazy Updating
By default, Livewire sends a request to the server after every input event (or change in some cases). This is usually fine for things like elements that don't typically fire rapid updates, however, this is often unnecessary for text fields that update as the user types.
In those cases, use the lazy directive modifier to listen for the native change event.
<input type="text" wire:model.lazy="message">
Now, the $message property will only be updated when the user clicks away from the input field.
Deferred Updating
In cases where you don't need data updates to happen live, Livewire has a .defer modifer that batches data updates with the next network request.
For example, given the following component:
<input type="text" wire:model.defer="query">
<button wire:click="search">Search</button>
As the user types into the field, no network requests will be sent. Even if the user clicks away from the input field and onto other fields on the page, no requests will be sent.
When the user presses "Search", Livewire will send ONE network request that contains both the new "query" state, AND the "search" action to perform.
This can drastically cut down on network usage when it's not needed.
The above is from: https://laravel-livewire.com/docs/2.x/properties

Related

Post/Redirect/Get needed if you submit HTML fragments?

In the past I used the Post/Redirect/Get pattern:
the html for was submitted to the server via POST
the server processed the data.
if everything was ok, the server responsed with a http 302 (redirect
the client redirected the page to the new location.
Is this still needed if you submit html fragments via htmx?
By and large no, you will not need to implement the PRG pattern.
Since htmx uses AJAX for most interactions, there is no request sitting in the browser history, and hitting refresh will not re-submit a POST (or DELETE or whatever).
That said, htmx trys to be compatible with the PRG pattern, and tries to update the URL if a redirect occurs by detecting the :
https://github.com/bigskysoftware/htmx/blob/1d4c79490e491813ffb780354ec5df6d080b1e09/src/htmx.js#L2146
https://github.com/bigskysoftware/htmx/blob/1d4c79490e491813ffb780354ec5df6d080b1e09/src/htmx.js#L1851
If you do something like inline editing:
https://htmx.org/examples/click-to-edit/
The point becomes moot to a large extent, since you can have the edit UI at the same URL as the view URL.

How to auto login to a new people soft profile using web logic

How to embed a user name and password into the URL so that the page will be
redirected without the user having to login
You can POST to your URL using:
<input id="userid" name="userid" type="text" value="PS" size="15" tabindex="2">
<input type="password" id="pwd" name="pwd" class="pure-input-2-3" size="15" tabindex="3" value ="PS">
Generally this is a bad idea because URL parameters are saved in browser history and are visible in the address bar, but it isn't right for us to judge your question since we don't know your use case. It is entirely possible that your solution is secure and safe and part of an automated process communicating through a secure channel.
PeopleSoft automatically understands POST parameters, so no special logic necessary for a POST request. A GET request is also possible, but requires some work on your end. The easiest way would be to use a ServletFilter or some other proxy program in front of PeopleSoft to rewrite the GET request as a POST request. Another alternative is to use a public user ID with a public iScript to SwitchUser. Signon PeopleCode may work as well.
If you create a process for logging in via GET, other users may have access to the same approach, so just be sure to secure your GET process as well.
Whatever you do, be extremely careful. I have known people to write Signon PeopleCode that ALWAYS evaluates to True. Obviously, this completely eliminates security. If you write Signon PeopleCode, be sure to keep it simple.

Overriding URL paths sent to Google Analytics

In my web application, I have a path as /search.
I also have a cookie named city set as CityA or CityB depending on what the user selected previously. I have set up Google Analytics to monitor the Visitor Flow.
My question is, how can I override the path /search to show up as /cityA/search or /cityB/search in the Analytics Behavior Flow menu, depending on the cookie value?
PS. It is a Rails app and actually changing the URL is not feasible at this point, since I will then have to reconfigure my Routes.rb file and update links everywhere.
Edit:
I have to use ga.js. Moving to Universal Analytics(analytics.js) is beyond my control at the moment.
In your analytics.js snippet you should see the following line:
ga('send', 'pageview');
You can pass an additional argument to the send method that overrides the page path. In your case it would look something like this:
ga('send', 'pageview', {
page: 'cityA/search'
});
You'd have to add some Rails logic in your .erb file to adjust the page value based on the cookie, but that shouldn't be too much trouble.
For reference, here's some information on the send method and the arguments it accepts:
https://developers.google.com/analytics/devguides/collection/analyticsjs/method-reference#send

How to monitor an action by user on Glass

I have a mirror API based app in which i have assigned a custom menu item, clicking on which should insert a new card. I have a bit of problem in doing that. I need to know of ways i can debug this.
Check if the subscription to the glass timeline was successful.
Print out something on console on click of the menu.
Any other way i can detect whether on click of the menu, the callback URL was called or not.
It sounds like you have a problem, but aren't sure how to approach debugging it? A few things to look at and try:
Question 1 re: checking subscriptions
The object returned from the subscriptions.insert should indicate that the subscription is a success. Depending on your language, an exception or error would indicate a problem.
You can also call subscriptions.list to make sure the subscriptions are there and are set to the values you expect. If a user removes authorization for your Glassware, this list will be cleared out.
Some things to remember about the URL used for subscriptions:
It must be an HTTPS URL and cannot use a self-signed certificate
The address must be resolvable from the public internet. "localhost" and local name aliases won't work.
The machine must be accessible from the public internet. Machines with addresses like "192.168.1.10" probably won't be good enough.
Question 2 re: printing when clicked
You need to make sure the subscription is setup correctly and that you have a webapp listening at the address you specified that will handle POST operations at that URL. The method called when that URL is hit is up to you, of course, so you can add logging to it. Language specifics may help here.
Try testing it yourself by going to the URL you specify using your own browser. You should see the log message printed out, at a minimum.
If you want it printed for only the specific menu item, you will need to make sure you can decode the JSON body that is sent as part of the POST and respond based on the operation and id of the menu item.
You should also make sure you return HTTP code 200 as quickly as possible - if you don't, Google's servers may retry for a while or eventually give up if they never get a response.
Update: From the sample code you posted, I noticed that you're either logging at INFO or sending to stdout, which should log to INFO (see https://developers.google.com/appengine/docs/java/#Java_Logging). Are you getting the logging from the doGet() method? This StackOverflow question suggests that appengine doesn't display items logged at INFO unless you change the logging.properties file.
Question 3 re: was it clicked or not?
Depending on the configuration of your web server and app server, there should be logs about what URLs have been hit (as noted by #scarygami in the comments to your question).
You can test it yourself to make sure you can hit the URL and it is logging. Keep in mind, however, the warnings I mentioned above about what makes a valid URL for a Mirror API callback.
Update: From your comment below, it sounds like you are seeing the URL belonging to the TimelineUpdateServlet is being hit, but are not seeing any evidence that the log message in TimelineUpdateServlet.doPost() is being called. What return code is logged? Have you tried calling this URL manually via POST to make sure the URL is going to the servlet you expect?

Link clicked in MS Word loses CF Session vars, but copied & pasted works fine

Been trying to figure this out for an hour now and I'm stymied. Simple site that allows employees to register. Typically the employer has a company wide u/p for all employees to use to access the registration page, but client also wanted a way to give employee a link to auto-login to register.
Simple enough - created a page "r.cfm" that looks for URL.emid (encrypted employer ID) and URL.h (5 character hash as a check based on the decrypted employer ID). A full URL may look something like this:
https://www.domain.com/r.cfm?emid=22EBCA&h=F5DEA
r.cfm makes sure the correct URL vars are there, decrypts the emid, compares the check value and if all is correct sets some session vars as such:
<cflock scope="session" type="exclusive" timeout="10">
<cfset SESSION.LOGGEDIN = TRUE/>
<cfset SESSION.LOGIN.EMPLOYEE.COID = DecryptString(url.emid)/>
</cflock>
I think use CFHEADER 302 and CFHEADER location to send them onto the next page. Here's where it gets weird. On the next page I setup some test code to e-mail me a dump of the session.
If clicked directly in MS Word I get to the 2nd page (the one from the cfheader redirect - employeeRegister.cfm) and I get not one - but two e-mail dumps of the session. The first one shows logged in as true, but the 2nd one shows it as false with a different jsessionid.
If I take the exact same link, paste it into my browser, it works as expected - one e-mail with a session dump showing that session.logged in true.
There is nothing on employeeRegister.cfm that would initiate a page reload. It actually doesn't even check the session.logged in var until the following page. employeeRegister.cfm is simply terms and conditions and a submit button to go to the next page, which is where the session vars are read and checked. It is literally a div with text and then a form tag with accept / decline.
This is because the office product initially tries to act as the browser (to test for web authoring) instead of handing off control to the browser right away. By the time the browser gets control of the url a valid session doesn't exist because office isn't going to share cookies. Without a valid session cookie you end up getting logged out during subsequent redirects or navigation of the site in question.
These MS KB article should help you solve the problem.
http://support.microsoft.com/kb/899927 <- mostly
http://support.microsoft.com/kb/218153 <- more info about Office links