CON-1199 - Render scripts in the bottom

Created by Jeffrey Schoemaker 27 Oct 2016, 13:51:27 Updated by Mads Krohn 23 Aug 2018, 08:46:00

Tags: Unscheduled Gold partner

Duplicates: U4-10685

Relates to: CON-917

Hi,

I have the same problem as stated in this our-post (https://our.umbraco.org/forum/umbraco-forms/78683-umbraco-adds-scripts-and-css-even-if-no-form-is-inserted).

I have this code on my page followed the guide on our (https://our.umbraco.org/documentation/products/umbracoforms/developer/Rendering-Scripts/):

@TempData["UmbracoForms"] @Umbraco.RenderMacro("FormsRenderScripts")

TempData["UmbracoForms"] is null until I reach a page with a Umbraco Form in a RTE. At that moment TempData["UmbracoForms"] is filled correctly with the GUID. If I then go to another page without a Umbraco Form on it. The TempData["UmbracoForms"] is still filled with the GUID and thus renders javascripts and css for Forms.

It looks like TempData["UmbracoForms"] is more sticky than supposed too.

Regards,

Jeffrey

Comments

Sebastiaan Janssen 04 Jan 2018, 13:52:39

Could it be that you render the TempData on a page that doesn't return a 200 OK?

From https://stackoverflow.com/questions/7630678/tempdata-not-being-cleared :

Below are some of the key points to note when using Temp data.

  1. A read access to temp data doesn't remove items from the dictionary immediately, but only marks for deletion.

  2. Temp data will not always remove the item that has been accessed. It only removes the item when an action results in an Http 200 status code (ViewResult/JsonResult/ContentResult etc).

  3. In case of actions that result in an Http 302 (such as any redirect actions), the data is retained in storage even when it is accessed.


Sebastiaan Janssen 04 Jan 2018, 13:54:47

This could possibly fix it, adding to the FormsRenderScripts macro

if(TempData["UmbracoForms"]) != null TempData["UmbracoForms"] = null;


Sebastiaan Janssen 04 Jan 2018, 14:00:13

So currently the RenderUmbracoFormScripts.cshtml looks like this:

@inherits Umbraco.Web.Macros.PartialViewMacroPage @using Umbraco.Forms.Web.Controllers

@if (TempData["UmbracoForms"] != null) { foreach (var form in (List)TempData["UmbracoForms"]) { Html.RenderAction("Render", "UmbracoForms", new ); } }

Update could be:

@inherits Umbraco.Web.Macros.PartialViewMacroPage @using Umbraco.Forms.Web.Controllers

@if (TempData["UmbracoForms"] != null) { // Reading TempData["UmbracoForms"] should have set it to null but this doesn't happen if this request didn't return a 200 OK status, explictly clear it so that on the next request the scripts don't get rendered if(TempData["UmbracoForms"]) != null { TempData["UmbracoForms"] = null; }

foreach (var form in (List<Guid>)TempData["UmbracoForms"])
{
    Html.RenderAction("Render", "UmbracoForms", new { formId = form, mode = "script"});
}

}

Would you have a chance to test this on an affected site @jeffrey.schoemaker@perplex.nl ?


Stefan Verleger 30 Jan 2018, 12:15:50

@sebastiaan Your Solutions helps with the issue http://issues.umbraco.org/issue/CON-1481 DatePicker scripts appearing in source twice.


suzyb 07 Feb 2018, 12:17:53

@sebastiaan I was having the same issue so implemented your solution. It does seem to fix the issue although I did have to move the code setting TempData to null to below the for loop where it was accessed.


Sebastiaan Janssen 22 Feb 2018, 11:14:58

@suzyb What for loop are we talking about?


Sebastiaan Janssen 22 Feb 2018, 11:15:59

Oh haha never mind, I see it now :)


Sebastiaan Janssen 22 Feb 2018, 11:30:12

Ended up with the following:

@inherits Umbraco.Web.Macros.PartialViewMacroPage @using Umbraco.Forms.Web.Controllers @{ var formsToRender = new List(); object forms = TempData["UmbracoForms"]; if (forms != null)

// Reading TempData["UmbracoForms"] should have set it to null but this doesn't
// happen if this request didn't return a 200 OK status. https://stackoverflow.com/a/7630709/5018
// Explictly clear it so that on the next request the scripts don't get rendered.
TempData.Remove("UmbracoForms");

}

@foreach (Guid form in formsToRender) { Html.RenderAction("Render", "UmbracoForms", new ); }


Mads Krohn 29 May 2018, 14:13:23

Just upgraded to Forms 7.0.3, and this issue is still present. Very easy to see if you have two browser tabs with the same site, and a form rendered in each tab. object forms = TempData["UmbracoForms"]; already contains 2 guids at that point, so forcefully removing the temp data does nothing. Umbraco version is 7.10.4.


Mads Krohn 29 May 2018, 14:16:30

Besides, looping the script rendering is a really bad idea, as it loads umbracoforms.js etc twice. In there is an ancient $('.contour input[type=submit]').not('.cancel').click() method that will gladly register twice, so you end up with a double submission if you ever loaded two forms on the same page anyways.


Gavin Faux 04 Aug 2018, 15:03:54

Seeing this on Umbraco 7.11.1, Forms 7.03 on cloud and localhost, view two different (GUID) forms in a row and formsToRender contains two values. First value is for previous form visited, second appears to be the current form, when both or wrong script is included the client has script issues with conditions and validations or worse (duplicate pikaday instances cause stack overflow and tab crashes).

If it makes any difference these forms sit behind default Membership provider; have not tested other scenarios.

Workaround for now:

`` @if (formsToRender.Any()) { Html.RenderAction("RenderFormScripts", "UmbracoForms", new );

if (formsToRender.Count > 1)
{
    LogHelper.Warn<PartialViewMacroPage>(string.Format("{0} form guids found to render scripts, only rendering last script. Guids are: {1}", formsToRender.Count, string.Join(";", formsToRender.Select(guid => guid.ToString()))));
}

} ``


Mads Krohn 23 Aug 2018, 08:46:00

@sebastiaan this is still an issue, could you open the ticket again? Also, Forms v. 7.0.3 isn't on the list of affected versions :)


Priority: Normal

Type: Bug

State: Fixed

Assignee: Sebastiaan Janssen

Difficulty:

Category:

Backwards Compatible: True

Fix Submitted:

Affected versions: 4.4.2, 7.0.2

Due in version: 6.0.7

Sprint:

Story Points:

Cycle: